[英]Why does null.asInstanceOf[<some CPS annotated type>] fail?
Is there any logical reason why null.asInstanceOf[<some CPS annotated type>]
fails to compile? 是否有任何逻辑上的原因,为什么null.asInstanceOf[<some CPS annotated type>]
无法编译?
For context, see this github issue thread . 有关上下文,请参阅此github问题线程 。
I wonder if the scala 2.9 version was doing the right thing even if it compiled (I'll try when I have a chance). 我想知道scala 2.9版本是否正在做正确的事情,即使它编译(我会尝试当我有机会)。 Anyway, in 2.10.0 this compiles and works: 无论如何,在2.10.0中这个编译和工作:
import scala.util.continuations._
object NullCPS extends App {
def f[A,C] = shiftUnit[A,C,C](null.asInstanceOf[A])
println(reset{"got " + f[Object, String]}) // prints: got null
println(reset{"got " + f[Int, String]}) // got 0
println(reset{"got " + f[Boolean, String]}) // got false
}
shitUnit[A,B,C]
is a library method from the continuations package that uses a value of type A
to create a trivial ControlContext[A,B,C]
expected to be used by a continuation k of type A=>B
eventually returning a result of type C
. shitUnit[A,B,C]
是一个来自continuations包的库方法,它使用类型A
的值来创建一个简单的ControlContext[A,B,C]
期望由类型A=>B
的连续k使用返回类型C
的结果。
In my example above "got " + f[Object, String]
, the continuation plugin further composes the trivial null
value with the (x:Object) => "got " + x
function. 在上面的例子中, "got " + f[Object, String]
,continuation插件进一步用(x:Object) => "got " + x
函数组成了普通的null
值。
Moving on to the context of the question , I cannot see how Defaultable[T]
can represent the T@cps[U]
since it only has one type parameter. 继续讨论问题的上下文 ,我看不出Defaultable[T]
如何表示T@cps[U]
因为它只有一个类型参数。 But if you enhance Defaultable
: 但是如果你增强了Defaultable
:
import scala.util.continuations._
trait DefCPS[A,C] { def default: A@cps[C] }
object NullCPS extends App {
implicit def defaultCPS[A,C] = new DefCPS[A,C] {
def default: A@cps[C] = shiftUnit[A,C,C](null.asInstanceOf[A])
}
println(reset{"got " + implicitly[DefCPS[Object,String]].default})
println(reset{"got " + implicitly[DefCPS[Int,String]].default})
println(reset{"got " + implicitly[DefCPS[Boolean,String]].default})
}
// prints got null
// got 0
// got false
Notes: I had to make the trait use a def
instead of val
or it would not compile. 注意:我必须使特性使用def
而不是val
,否则它将无法编译。 I also tried materializing a A@cpsParam[B,C]
but was not able. 我也试过实现A@cpsParam[B,C]
但是没能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.