scala nested case classes generate strange type error -
i'm trying use case class has 1 attribute reference case class. there conditions construct object strange type errors.
so works fine.
case class foo(a:int) case class bar(b:foo, c:foo) val t = bar(foo(1),foo(2)) //t: bar = bar(foo(1),foo(2))
when nest object, there doesn't seem problem
object w { case class foo(a:int) case class bar(b:foo, c:foo) }
i can create object
val t = w.bar(w.foo(1),w.foo(2))
however, when try construct object defined foo, gives me crazy type error.
val f = w.foo(1) w.bar(f,f) // error: type mismatch; // found : w.foo // required: w.foo // w.bar(f,f)
any ideas? scala 2.10.5
this can happen if try on console, while redefine of classes.
if type in console:
scala> :paste // entering paste mode (ctrl-d finish) case class foo(a:int) case class bar(b:foo, c:foo) val t = bar(foo(1),foo(2)) object w { case class foo(a:int) case class bar(b:foo, c:foo) } // exiting paste mode, interpreting. defined class foo defined class bar t: bar = bar(foo(1),foo(2)) defined module w scala> val t = w.bar(w.foo(1),w.foo(2)) t: w.bar = bar(foo(1),foo(2)) scala> val f = w.foo(1) f: w.foo = foo(1) scala> w.bar(f,f) res1: w.bar = bar(foo(1),foo(1))
it work. however, if redefine w object fail:
scala> object w { | case class foo(a:int) | case class bar(b:foo, c:foo) | } defined module w scala> w.bar(f,f) <console>:14: error: type mismatch; found : w.foo(in object w)(in object w)(in object w)(in object w) required: w.foo(in object w)(in object w)(in object w)(in object w) w.bar(f,f)
this happen since @ w.bar(f,f)
in second w, use w define on second type, f define in first time. since different object (same name , properties, type different), not fit.
if redefine depend class (f) works:
scala> object w { | case class foo(a:int) | case class bar(b:foo, c:foo) | } defined module w scala> val f = w.foo(1) f: w.foo = foo(1) scala> w.bar(f,f) res3: w.bar = bar(foo(1),foo(1))
Comments
Post a Comment