Skip to content

Union types are approximated too often, especially in the REPL #1532

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

Closed
XuefengWu opened this issue Sep 22, 2016 · 3 comments
Closed

Union types are approximated too often, especially in the REPL #1532

XuefengWu opened this issue Sep 22, 2016 · 3 comments

Comments

@XuefengWu
Copy link

union type works well in container type.

scala> Seq[Int|Double](1)
res25: ([+X0] -> scala.collection.Seq[X0])[Int | Double] = List(1)
scala> Seq[Int|Double](1)(0)
res26: Int | Double = 1
scala> if(true) then Seq[Int|Double](1)(0) else Seq[Int|Double](1.1)(0)
res27: Int | Double = 1

but if union type value is not in container, it's result type is Any

scala> 1: Int | String
res0: Any = 1

and it also doest work for method.

  def f(x: Int): String | Int | Double = {
  if(x == 1) x.toString 
  else if (x == 2) x.toInt  
  else x.toDouble }

f: (x: Int)String | Int | Double
scala> f(1)
res1: Any = 1

the result type should be union type.

@smarter
Copy link
Member

smarter commented Sep 22, 2016

Yes, we widen union types too much as discussed in #1495

For now, you can run the compiler with -language:keepUnions (though there's a reason this is not the default, see http://guillaume.martres.me/talks/dotty-tutorial/#/1/13 (use the ↓ arrow key to see the full slide)), you can run the repl with arguments like this: dotc -repl -language:keepUnions

@XuefengWu
Copy link
Author

Thank you for explanation.

On Thu, Sep 22, 2016 at 11:05 PM, Guillaume Martres <
[email protected]> wrote:

Yes, we widen union types too much as discussed in #1495
#1495

For now, you can run the compiler with -language:keepUnions (though
there's a reason this is not the default, see http://guillaume.martres.me/
talks/dotty-tutorial/#/1/13 (use the ↓ arrow key to see the full slide)),
you can run the repl with arguments like this: dotc -repl
-language:keepUnions


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#1532 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAJ3pa8gcc7PdqopQvngjwbZNZPsA9Sbks5qspkdgaJpZM4KD_L9
.

~Yours, Xuefeng Wu/吴雪峰 敬上

@smarter smarter changed the title value with union type, but result type is Any Union types are widened too eagerly, especially in the REPL Sep 22, 2016
@smarter smarter changed the title Union types are widened too eagerly, especially in the REPL Union types are approximated too often, especially in the REPL Sep 22, 2016
@smarter
Copy link
Member

smarter commented Jan 11, 2018

Closing since this is unlikely to change for now. As a workaround you can define an identity type and use Identity[Foo | Bar] instead of Foo | Bar.

scala> type Identity[T] = T 
// defined alias type Identity = [T] => T
scala> 1: Identity[Int | String] 
val res1: Identity[Int | String] = 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants