简体   繁体   English

按名称调用参数

[英]call-by-name parameter

anybody can give me explain how to work call-by-name inside of the scala compiler?任何人都可以给我解释如何在 Scala 编译器中按名称工作? 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?该方法的语法快捷方式将是这样的: arg: =>Int将转换为arg: () => Int并捕获传递给函数的参数,如何闭包? ie real type of call-by-name parameter such: Function0[_] ?即按名称调用参数的真实类型,例如: Function0[_] ?

Thanks.谢谢。

Call-by-name parameters, as you yourself have discovered, desugar down to Function0[A] .正如您自己所发现的那样,按名称调用参数将降糖降为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.您可以使用 Scalaz 的Name沿着这些方向实现一些目标。 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.如果我正确理解了这个问题,是的,语法=> Int本质上是匿名无参数函数() => Int的轻量级语法,它在 Scala 中由Function0[Int]类型表示。 Furthermore, within the VM by-name parameters translate to inner classes.此外,在 VM 中按名称参数转换为内部类。

A by-name parameter's type is parameterless method (see @som-snytt's comment).按名称参数的类型是无参数方法(请参阅@som-snytt 的注释)。 It cannot be used on Scala 2.x anywhere except on a method's parameter type.除了方法的参数类型之外,它不能在 Scala 2.x 的任何地方使用。

You can use a Function0 in place of a parameter that is being passed by-name, but that has different semantics.您可以使用Function0代替按名称传递的参数,但具有不同的语义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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