Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type equivalence issue when using dependent method types and the type projections

I'm trying the following with 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
}

The problem is the same when using this form:

def bar[C <: Container](c: C)(x: C#X): c.X = x

I don't really see why foo compiles while bar does not.

I believe that c.X and C#X should be the same here.

Also, I don't understand the error message:

[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

Any idea?

like image 536
betehess Avatar asked Mar 09 '26 07:03

betehess


1 Answers

C#X means an X from any C. c.X means an X from your particular C, namely c. The latter is much more specific!

For example, if X is a bill and c is a particular customer, c.X means that the method only accepts bills from (for, presumably) customer c. C#X means it accepts any bill from any customer. If you want to ensure that customers only get charged with their own bills (at least by default), the former is what you want.

like image 186
Rex Kerr Avatar answered Mar 12 '26 00:03

Rex Kerr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!