简体   繁体   中英

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? ie real type of call-by-name parameter such: Function0[_] ?

Thanks.

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 .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM