gpt4 book ai didi

scala - 为什么 ((_ : Int, _ : Int) => _/_) not compile when ((_: Int)/(_: Int)) does?

转载 作者:行者123 更新时间:2023-12-04 02:40:21 26 4
gpt4 key购买 nike

我正在学习 Scala,有一个非常基本的问题。考虑以下两个使用占位符语法的表达式 -

// Syntax A  
val fnA = (_: Int, _: Int) => _ / _

// Syntax B
val fnB = (_: Int) / (_: Int)

及其尝试的应用 -

// Syntax A
fnA(33, 3)

// Syntax B
fnB(33, 3)

在这两个中,只有 B 和 App(B) 是有效的语法,我不确定为什么。如果编译器能够推断出 fnB 中的参数(以及应用它们的顺序),为什么它不能为 fnA 做到这一点?我提出问题的前提是 fnBfnA 的简写形式,我很确定这种推理存在缺陷。我只是不确定缺陷是什么。

最佳答案

箭头左边下划线的含义=>

(_: Int, _: Int) => ...

与中缀运算符周围的下划线的含义不同 /

(_: Int) / (_: Int) 

前者的意思是

"We are defining a function that takes two arguments, however we will not use these arguments in the body of the function"

而后者是定义两个参数的函数的简写

(x: Int, y: Int) => x / y

例如,

(_: Int, _: Int) => (_: Int) / (_: Int)

脱糖成类似的东西

(a: Int, b: Int) => ((x: Int, y: Int) => x / y)

我们在哪里看到参数 ab正文中未使用恰好是另一个函数 ((x: Int, y: Int) => x / y) .

Daniel将这两种含义记录为

_ + _             // Anonymous function placeholder parameter
_ => 5 // Discarded parameter

作为旁注,考虑在涉及类型构造函数及其边界的类型级别上有点类似的情况,其中下划线的含义取决于上下文

CC[_] <: Iterable[_]  

就我个人而言,我的想法欺骗我认为它等同于

CC[x] <: Iterable[x]  

然而事实并非如此,<:左边下划线的意思与右边的意思不同

CC[x] <: Iterable[y] forSome {type y}

注意如何 x没有出现在 Iterable[y] forSome {type y} 中. Adrian 在 Common Pitfalls 下记录了这一点:

...perhaps surprisingly, CC[_] <: Traversable[_] is not equivalent to CC[X] <: Traversable[X]. The former expands to CC[X] <: Traversable[T] forSome {type T}, where T is existentially bound and thus unrelated to X.

comment 中至 type bounds on type constructor

comparing the level of types and values, it is quite unfortunate that the type-level underscore behaves differently from the value-level underscore. At the value level, _ + _ indeed stands for (x, y) => x + y, a function. As hinted at in my other comment, the type-level underscore is context-sensitive, and it never introduces a type-level function. It's either an anonymous type parameter definition or an anonymous existential. Neither of these have value-level equivalents.

因此,我们应该小心地在其上下文中解释下划线的含义。

关于scala - 为什么 ((_ : Int, _ : Int) => _/_) not compile when ((_: Int)/(_: Int)) does?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59698657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com