使用依赖方法类型和类型投影时,键入等效问题

bet*_*ess 2 scala path-dependent-type dependent-method-type

我正在使用Scala 2.10.0-M1尝试以下内容:

trait Container {
  type X
}

class Test[C <: Container](val c: C) {
  def foo(x: c.X): C#X = x // this compiles fine
  def bar(x: C#X): c.X = x // this does not compile
}
Run Code Online (Sandbox Code Playgroud)

使用此表单时问题是相同的:

def bar[C <: Container](c: C)(x: C#X): c.X = x
Run Code Online (Sandbox Code Playgroud)

我真的不明白为什么foo编译而bar不是.

我相信,c.XC#X应该是一样的在这里.

另外,我不明白错误信息:

[error]  found   : x.type (with underlying type C#X)
[error]  required: Test.this.c.X
[error]  possible cause: missing arguments for method or constructor
[error]   def bar(x: C#X): c.X = x // this does not compile
Run Code Online (Sandbox Code Playgroud)

任何的想法?

Rex*_*err 7

C#X意味着X来自任何人C. c.X意思是X来自你的特殊C,即c.后者更具体!

例如,如果X是账单并且c是特定客户,则c.X意味着该方法仅接受来自(对于可能的)客户的账单c. C#X意味着它接受任何客户的任何账单.如果您想确保客户只收取自己的账单(至少默认情况下),前者就是您想要的.