This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Sealed traits

2 replies
Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.

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-

Mirko Stocker
Joined: 2009-09-10,
User offline. Last seen 45 weeks 6 days ago.
Re: Sealed traits

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

Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
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
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland