eclipse-archived/ceylon

unify value and type cases in 'switch'

Closed this issue · 10 comments

[@gavinking] In an of clause we write stuff like of x | y | Type. Why can't we write that in a case?

Currently Ceylon forces you to distinguish type cases from value cases using the is keyword, which very occasionally pushes you towards nastiness like this:

case (is \Itrue | \Inull | Foo) { … }

Are there any really compelling reasons to not let you just write:

case (true | null | Foo) { … }

Historically, I think there were some reasons, since I had speculated about a lot of different kind of case conditions, case (satisfies ... ), etc, which we still don't have. Also the syntax for value cases was different to the syntax for union types with the ,s instead of |s.

Right now, I can't really think of much.

[Migrated from ceylon/ceylon-spec#1296]

[@luolong] I think it might have been an artifact of treating case's as conditionals and thus making them closer in syntax with if statements.

Also, wasn't is Foo supposed to be doing some sort of type narrowing along the way?

[@gavinking]

I think it might have been an artifact of treating case's as conditionals and thus making them closer in syntax with if statements.

Well, they're not defined as conditions in the spec.

Also, wasn't is Foo supposed to be doing some sort of type narrowing along the way?

Well, sure, that's what it does, of course, but now even value cases affect flow-sensitive typing (they narrow the else clause).

Hrmph, well the grammar/typechecker side of this work turned out to have been surprisingly easy.

Next up: let's see if it's easy to change the Java backend to support this.

This work is on the 4402 branch, FTR.

Note to self: need to be careful here that I don't step on #6148.

How does one syntactically distinguish a type case from object destructuring?

I suppose we're OK as long as we don't allow destructuring for nullary classes, i.e. case (Person()) is a type case with a function type, not a pattern.

Looks like I now have this basically-working on the JVM.

And now the JS backend is done. I think this is good enough to merge. Obviously, I'm still missing:

  • specification
  • typechecker tests
  • Java backend tests
  • JS backend tests

I have added tests for the Java backend. Not sure how to write tests for this for JS.

Spec is done.

Closing. Done.