加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

解析 – Scala 2.9中的错误或奇怪的行为

发布时间:2020-12-16 18:21:58 所属栏目:安全 来源:网络整理
导读:请注意以下奇怪的行为( Scala 2.9.1.RC2): scala val spam = x = log(x)spam: Double = Double = function1scala val spam = x = log(x)*log(x)console:10: error: missing parameter type val spam = x = log(x)*log(x) ^scala log(2)*log(2)res30: Double
请注意以下奇怪的行为( Scala 2.9.1.RC2):

scala> val spam = x => log(x)
spam: Double => Double = <function1>

scala> val spam = x => log(x)*log(x)
<console>:10: error: missing parameter type
       val spam = x => log(x)*log(x)
                  ^

scala> log(2)*log(2)
res30: Double = 0.4804530139182014

为什么Scala可以推断出第一个的类型而不是第二个?

另一种陌生感:

scala> def eggs(foo:Int=-1) = foo
<console>:1: error: identifier expected but integer literal found.
       def eggs(foo:Int=-1) = foo
                         ^

scala> def eggs(foo:Int= -1) = foo
eggs: (foo: Int)Int

这里发生了什么?当=和 – 之间没有空格时,为什么会窒息?

解决方法

问题1.对我来说,惊喜是类型推断成功.另一个无法编译的情况是,

val spam = x => log(log(x))

通常,规则是必须使参数类型(此处为x)显式化.但显然这条规则并不适用于特殊情况x => f(x),它被重写为f _.在其他情况下,this rewriting leads to unspec’ed behavior.

注意:如果存在预期的函数类型,则参数类型不必显式,

val spam: Double => Double = x => log(log(x)) // OK

问题2.没有空格,您遇到了类型的“运算符”语法.这是一个编译的例子,

trait =-[A,B]
trait One
def eggs(foo: Int=-One) = foo

这相当于,

def eggs(foo: =-[Int,One]) = foo

您获得的错误消息(标识符但是…)表示整数文字1不是有效类型.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读