scala - Pattern Match Abstract Type Trait Member -
sealed trait foo { type t <: option[any] val x : t } case class bar(x : option[int]) extends foo { type t = option[int] } val baz : foo = bar(some(42)) baz.x match { case some(a) => case none => 1337 }
this error message when attempting pattern match:
:12: error: pattern type incompatible expected type; found : none.type required: baz.t
i believe due type-erasure on type t.
i'm not sure why fails, limitation in type inference. following works:
(baz.x: option[any]) match { case some(a) => case none => 1337 }
in case, asking subtype of option
not make sense. better define option's element type:
sealed trait foo { type def x: option[a] } case class bar(x : option[int]) extends foo { type = int } val baz : foo = bar(some(42)) baz.x match { case some(a) => case none => 1337 }
Comments
Post a Comment