Scala是否具有投影嵌套单例类型的语法?
class C { object O } val x: C#O.type = (new C).O // error: ';' expected but '.' found val y: C#(O.type) = (new C).O // error: identifier expected but '(' found 这种类型有合法的语法吗? 编辑: val x: c.O.type forSome { val c: C } = (new C).O 给 ScalaFiddle.scala:4: error: type mismatch; found : ScalaFiddle.this.C#O.type required: c.type#O.type forSome { type c.type <: ScalaFiddle.this.C with scala.this.Singleton{} } val x: c.O.type forSome { val c: C } = (new C).O ^ 因此Scala编译器理解这种类型并将其显示为C#O.type.在创建语法时,似乎忽略了这种情况. 解决方法
概观
>几次尝试写下这种类型 我无法证明这是不可能的,但至少我想确保我没有尝试就不放弃. 几次尝试写下这种类型 系统地重写AlexeyRomanov的看起来最有希望的尝试: val o: x.O.type forSome { val x: C } = (new C).O val o: x.type#O.type forSome { val x : C } = (new C).O val o: t#O.type forSome { type t <: C with Singleton } = (new C).O 使用类型lambda类构造的一些或多或少的非系统尝试: // boring,does nothing,identity,just to recall lambda-syntax val o: ({ type R <: c.O.type forSome { val c: C } })#R = (new C).O val o: ({type L[A <: C] = c.O.type forSome { val c: A }})#L[c.type forSome{val c: C}] = (new C).O val o: ({type L[A <: C] = c.O.type forSome { val c: A }})#L[A forSome {type A <: C}] = (new C).O 使用嵌套forSome进行实验: val o: x.type forSome { val x: c.O.type forSome { val c: C }} = (new C).O 在C中使用其他类型成员的实验: class C { object O type T = O.type } val o: C#T = (new C).O 这实际上编译,但修改右侧,所以我想它不计算: val o: c.O.type forSome { val c: C } = { val c = new C; c.O } 结论:对我来说不可能. 生成所有错误消息的Bash脚本 要生成所有错误消息(没有文件路径和功能警告),
#!/bin/bash tempFile=newCdotO.scala inputFile=saved_post.txt grep -oE "^ *val o:.*$" $inputFile | while read codeLine do printf '=%.0s' {0..80} echo "" echo "" > $tempFile echo "import scala.language.existentials" >> $tempFile echo "import scala.language.higherKinds" >> $tempFile echo "class C { object O; type T = O.type }" >> $tempFile echo "$codeLine" | tee -a $tempFile printf -- '-%.0s' {0..80} echo "" scala $tempFile 2>&1 | sed 's|^.*error:|error:|g' done | awk '{print " "$0}' 这会生成以下错误消息墙(嘿,我已经尝试清理它了!): ================================================================================= val o: x.O.type forSome { val x: C } = (new C).O --------------------------------------------------------------------------------- error: type mismatch; found : this.C#O.type required: x.O.type forSome { val x: this.C } val o: x.O.type forSome { val x: C } = (new C).O ^ one error found ================================================================================= val o: x.type#O.type forSome { val x : C } = (new C).O --------------------------------------------------------------------------------- error: ';' expected but '.' found. val o: x.type#O.type forSome { val x : C } = (new C).O ^ one error found ================================================================================= val o: t#O.type forSome { type t <: C with Singleton } = (new C).O --------------------------------------------------------------------------------- error: ';' expected but '.' found. val o: t#O.type forSome { type t <: C with Singleton } = (new C).O ^ one error found ================================================================================= val o: ({ type R <: c.O.type forSome { val c: C } })#R = (new C).O --------------------------------------------------------------------------------- error: type mismatch; found : this.C#O.type required: AnyRef{type R <: c.type#O.type forSome { type c.type <: this.C }}#R val o: ({ type R <: c.O.type forSome { val c: C } })#R = (new C).O ^ one error found ================================================================================= val o: ({type L[A <: C] = c.O.type forSome { val c: A }})#L[c.type forSome{val c: C}] = (new C).O --------------------------------------------------------------------------------- error: type mismatch; found : this.C#O.type required: c.type(in type L)#O.type forSome { type c.type(in type L) <: c.type(in value o) forSome { type c.type(in value o) <: this.C with Singleton } with Singleton } val o: ({type L[A <: C] = c.O.type forSome { val c: A }})#L[c.type forSome{val c: C}] = (new C).O ^ one error found ================================================================================= val o: ({type L[A <: C] = c.O.type forSome { val c: A }})#L[A forSome {type A <: C}] = (new C).O --------------------------------------------------------------------------------- error: type mismatch; found : this.C#O.type required: c.O.type forSome { val c: A forSome { type A <: this.C } } val o: ({type L[A <: C] = c.O.type forSome { val c: A }})#L[A forSome {type A <: C}] = (new C).O ^ one error found ================================================================================= val o: x.type forSome { val x: c.O.type forSome { val c: C }} = (new C).O --------------------------------------------------------------------------------- error: type mismatch; found : this.C#O.type required: x.type forSome { val x: c.type#O.type forSome { type c.type <: this.C } } val o: x.type forSome { val x: c.O.type forSome { val c: C }} = (new C).O ^ one error found ================================================================================= val o: C#T = (new C).O --------------------------------------------------------------------------------- error: type mismatch; found : this.C#O.type required: _1.O.type forSome { val _1: this.C } val o: C#T = (new C).O ^ one error found ================================================================================= val o: c.O.type forSome { val c: C } = { val c = new C; c.O } --------------------------------------------------------------------------------- 编辑2018-04-11: 偶然发现this here in 6.4 of the specification:
描述“not-other”表达式的句子之前的部分似乎是指普通名称和稳定标识符.鉴于规范中的这一条款,我并不完全清楚为什么(新的C).O的输入方式与{val y = new C;完全相同; y.O},因为在这次重写之后,有问题的代码 val o: c.O.type forSome { val c: C } = (new C).O 看起来就像我在上述一系列尝试中提出的唯一可行的建议: val o: c.O.type forSome { val c: C } = { val c = new C; c.O } 推断类型C#O.type实际上是否是一个错误,并且某些{val c:C}的c.O.type是规范规定的类型?我不会到目前为止声称这是一个错误,我对规范中使用的配方不够熟悉. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |