简体   繁体   English

“推断函数中使用的类型是文字”吗?

[英]“Infer the types used in a function are literals”?

There's an example in "Scala in Depth" where the author is explaining how scala can do some level of inference on the arguments passed into the methods. 在“深度缩放”中有一个示例,作者在其中说明了scala如何对传递给方法的参数进行某种程度的推断。 As an example the following is shown: 作为示例,显示了以下内容:

def myMethod(functionLiteral: A => B):Unit
myMethod({ arg:A => new B})
myMethod({ arg => new B})

Just to figure out what the author is talking about, I do the following in the REPL: 为了弄清楚作者在说什么,我在REPL中执行以下操作:

def myMethod(functionLiteral: Boolean => Boolean):Unit = {}
myMethod({a:Boolean => true})
myMethod({a => true})

The only revelatory thing that happens here is that the compiler doesn't throw an error. 唯一发生的启示是编译器不会抛出错误。

Is the author trying to say that the function argument a is inferred to be a Boolean by the compiler? 作者是否要说编译器将函数参数a推断为布尔值?

Yes the author is saying that dont need to specify that a is a Boolean in myMethod({a => true}) because the type is Boolean => Boolean 是笔者说,不需要指定aBooleanmyMethod({a => true})因为该类型是Boolean => Boolean

== Original answer which makes the first bit compile but misses the point a bit == ==原始答案,使第一位编译,但略有遗漏==

It needed to Be typed with [A,B] . 需要用[A,B]键入。

def myMethod[A,B](functionLiteral: A => B): Unit = {}
myMethod((arg:String) => arg.length)
myMethod((arg:Int) => (1 to arg).map(_ *2))

I modified it to return the function so you can see the types in the repl. 我对其进行了修改以返回该函数,以便您可以在repl中查看类型。

scala> def myMethod[A,B](functionLiteral: A => B): A => B = functionLiteral
myMethod: [A, B](functionLiteral: (A) => B)(A) => B

scala> myMethod((arg:String) => arg.length)
res11: (String) => Int = <function1>

scala> res11("hello world!")
res12: Int = 12

scala> myMethod((arg:Int) => (1 to arg).map(_ *2))
res13: (Int) => scala.collection.immutable.IndexedSeq[Int] = <function1>

scala> res13(4)
res14: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6, 8)

Is the author trying to say that the function argument a is inferred to be a Boolean by the compiler? 作者是否要说编译器将函数参数a推断为布尔值?

Absolutely. 绝对。 Given the following method: 给出以下方法:

def myMethod(functionLiteral: Boolean => Boolean):Unit = {}

The compiler knows that the parameter to myMethod is a function that takes a boolean parameter, so it does not need you to specify it. 编译器知道myMethod的参数是一个带有布尔参数的函数,因此不需要您指定它。 In other words, in the following a is unambiguously a boolean parameter: 换句话说,以下a无疑是布尔参数:

myMethod{a => true}

Now, it is worth noting that this does not work anymore when mixed with overloading: 现在,值得注意的是,当与重载混合时,这将不再起作用:

def myMethod(functionLiteral: Boolean => Boolean):Unit = {}
def myMethod(functionLiteral: Int => Boolean):Unit = {}
myMethod{a => true} // error: missing parameter type

The reason is that it cannot unambiguously tell if a is of type Boolean or Int . 原因是它无法明确判断aBoolean还是Int类型。

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

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