-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Using union types together with literal singleton types #1495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
|
@felixmulder Your example works fine on the latest master, probably fixed by #1491 |
The example in question was: object Test {
object Monday
object Tuesday
object Wednesday
object Thursday
object Friday
object Saturday
object Sunday
type Weekday = Monday.type | Tuesday.type | Wednesday.type | Thursday.type | Friday.type
type Weekend = Saturday.type | Sunday.type
type AnyDay = Weekday | Weekend
def main(args: Array[String]): Unit = {
println("Monday is weekday: " + Monday.isInstanceOf[Weekday])
println("Saturday is weekend: " + Saturday.isInstanceOf[Weekend])
println("Sunday is weekday: " + Sunday.isInstanceOf[Weekday])
(Monday: AnyDay) match {
case _: Weekend => println("shouldn't match")
}
}
} Which before #1491 would yield:
|
See #1551 |
This comment by Felix Smarter describes the performance concerns in more detail. |
Closing this since it is unlikely to be changed, witness #1532 |
Fixed by #6299 🎉 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a follow up on a gitter conversation with @smarter
I'm getting accustomed to some very handy uses of union types and literal singleton types in the Typescript part of our application. I've been trying this and some other TS uses of union types my beloved Scala with Dotty.
Things don't seem to work quite yet.
The most useful one is using union types with literal singleton types.
At the moment that combination doesn't seem to work very much.
That's a pity because there's at least one use case that's very useful. It's possible to encode enumerations to constrain function arguments and assignments.
It's something that has just appeared in Typescript 2.0 and it's really really useful.
I understood from @smarter that there's a concern around the performance of inferring return types:
Another feature I use a lot in Typescript is working with non nullable types.
Instead of using an
Option[A]
you use aA | Null
kind construction. Using a compiler flag or language feature import it would be possible to disallow null asignment.Currently the
Null
type seems to be thrown away.Here's the relevant TS doc
I think it's cool to have an alternative for the Option monad. But if it's hard to implement I'm perfectly fine with using
Option[A]
.Invoking a function on an object with a union type doesn't work right now with Dotty
The example is from Typescript docs
I guess this would be mostly useful for Scalajs since its compile target is very duck typing oriented.
For me and the Scala developers I've spoken to its the combination of union types and literal singleton types that's most valuable.
Btw I was amazed how quickly I got a response on gitter. On a Sunday! 😃
The text was updated successfully, but these errors were encountered: