Re: sealed
Rich Oliver <[email protected]> Thu, 11 Feb 2016 18:49:09 -0800 (PST)
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <[email protected]> |
That still leaves the sealed modifier less powerful than it could be. So if
we have
sealed trait A
trait A1 extends A
trait A2 extends A
trait A3 extends A
we can pattern match
def myA(a: A) a match
{
case A1 => ..........
case A2 => ........
case A3 => .........
}
and be certain that we've covered all cases and the match won't fall
through. But note this doesn't mean that other traits extending A is a
problem as long as no class is instantiated that doesn't inherit from at
least one of A1, A2 or A3.
In fact in some cases it would be nice to be to safely pattern match across
a multiple dimensions.
So
trait B extends A
trait B1 extends B
trait B2 extends B
and we could also guarantee that
def myB(b: B) b match
{
case B1 => .....
case B2 => .....
}
is also a safe match. But with Union and intersection types we can do this.
So it is my proposal that with union types, sealed types are removed form
the language. So we have:
trait A
{
self => A1 U A2 U A3
}
And we can even do guarantee the multiple dimension match with
trait A
{
self => (A1 U A2 U A3) & (B1 U B2)
}
Just to clarify I've used "U" for type union and "&" for type intersection.
Related to this I'm now wondering what the reason was for making Left and
Right final.
On Thursday, 11 February 2016 18:32:59 UTC, Oliver Ruebenacker wrote:
>
>
> Hello,
>
> I love the feature of having sealed types that can only be directly
> extended by a limited set of sub-types. However, I find it quite awkward
> that the allowed direct sub-types are those that are in the same file.
> First, this collides almost always with my preferences of what I would like
> to put in the same file and what not. Second, it requires careful reading
> of the entire file to find them all.
>
> Why not list the allowed sub-types, for example like this:
>
> sealed(B, C, D) trait A
>
> meaning that A can only be directly extended by B, C and D.
>
> Best, Oliver
>
> --
> Oliver Ruebenacker
> Senior Software Engineer, Diabetes Portal
> <http://www.type2diabetesgenetics.org/>, Broad Institute
> <http://www.broadinstitute.org/>
>
>
--
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.