: vs : =>

mProg
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 147
Registriert: 25. Apr 2015 00:10

: vs : =>

Beitrag von mProg »

In the solution for Ex01.2 the parameters of the Logger trait functions are declared as (msg: => String). What is the difference between (msg: => String) and (msg: String)?

ManuelWeiel
Mausschubser
Mausschubser
Beiträge: 72
Registriert: 7. Okt 2014 11:38

Re: : vs : =>

Beitrag von ManuelWeiel »

=> String means that this parameter is passed by name.

The following quote nicely summarises the difference:
A parameter of type A acts like a val (its body is evaluated once, when bound) and one of type => A acts like a def (its body is evaluated whenever it is used).
More details on this topic can be found here: https://tpolecat.github.io/2014/06/26/call-by-name.html

Antworten

Zurück zu „Archiv“