dvigal
dvigal

Reputation: 165

call-by-name parameter

anybody can give me explain how to work call-by-name inside of the scala compiler? Syntactic shortcut for that method will be such: arg: =>Int will be transformed to arg: () => Int and captures the passed parameter to the function, how a closure? i.e real type of call-by-name parameter such: Function0[_]?

Thanks.

Upvotes: 2

Views: 669

Answers (3)

Daniel C. Sobral
Daniel C. Sobral

Reputation: 297155

A by-name parameter's type is parameterless method (see @som-snytt's comment). It cannot be used on Scala 2.x anywhere except on a method's parameter type.

You can use a Function0 in place of a parameter that is being passed by-name, but that has different semantics.

Upvotes: 1

missingfaktor
missingfaktor

Reputation: 92016

Call-by-name parameters, as you yourself have discovered, desugar down to Function0[A]. They do not have a first class support, and thus no different type as such.

You can kind of achieve something along those lines using Name from Scalaz. For that, see the accepted answer here.

Upvotes: 4

elk
elk

Reputation: 472

If I understand the question correctly, yes the syntax => Int is essentially a lightweight syntax for anonymous parameterless function () => Int, that is represented by type Function0[Int] in Scala. Furthermore, within the VM by-name parameters translate to inner classes.

Upvotes: 3

Related Questions