- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Sealed traits
Thu, 2010-06-03, 18:30
hi,
why doesn't the compiler say the match is not exhaustive?
scala> sealed trait X; trait Y extends X; trait Z extends X
defined trait X
defined trait Y
defined trait Z
scala> def test( x: X ) = x match {
| case y: Y => 33
| }
test: (x: X)Int // <--- no error?
this also happens with compiled code (i.e. outside the REPL). scala 2.8.0.RC2
best, -sciss-
Thu, 2010-06-03, 20:07
#2
Re: Sealed traits
of course it makes sense to have sealed traits! it seems the problem occurs when i extend with traits instead of classes, e.g.
sealed trait X; class Y extends X; class Z extends X
def test( x: X ) = x match { case y: Y => 33 }
warning: match is not exhaustive!
missing combination Z
def test( x: X ) = x match { case y: Y => 33 }
^
test: (x: X)Int
but i don't see why the compiler should behave different here? anyway, since the top-most sealed thing can be a trait, i should be able to work around with abstract subclasses.
best, -sciss-
Am 03.06.2010 um 19:47 schrieb Mirko Stocker:
> On Thursday 03 June 2010 19:29:57 Sciss wrote:
>> why doesn't the compiler say the match is not exhaustive?
>
> The way I see it, this is as specified, but maybe unexpected. The Scala
> Language Specification §8.4 says:
>
> If the selector of a pattern match is an instance of a sealed class (§5.2),
> the compilation of pattern matching can emit warnings..
>
> Does it even make sense to have sealed traits? The specification also says that
> "The sealed modifier applies to class definitions. "
>
> Cheers
>
> Mirko
>
On Thursday 03 June 2010 19:29:57 Sciss wrote:
> why doesn't the compiler say the match is not exhaustive?
The way I see it, this is as specified, but maybe unexpected. The Scala
Language Specification §8.4 says:
If the selector of a pattern match is an instance of a sealed class (§5.2),
the compilation of pattern matching can emit warnings..
Does it even make sense to have sealed traits? The specification also says that
"The sealed modifier applies to class definitions. "
Cheers
Mirko