scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.79k stars 1.04k forks source link

Unions of singletons types don't work sometimes with the `->` operator #14183

Open chadselph opened 2 years ago

chadselph commented 2 years ago

Compiler version

3.1.0

Minimized code

Union types of singletons can't be used as keys to a Map.

type Row = 1 | 2 | 3
val board: Map[(Row, Char), String] = Map((1, 'a') -> "")

technically I guess this is slightly shorter and also produces the error:


val b6: ((Row, Char), String) = ((1, 'a') -> "")

Output

Found:    ((Int, Char), String)
Required: ((Row, Char), String)
  val board: Map[(Row, Char), String] = Map((1, 'a') -> "")

Expectation

I expect to be able to use this type here. Note that all the following code does compile:

type Row = 1 | 2 | 3
val square: (Row, Char) = (1, 'a')
val squares: List[(Row, Char)] = List((1 -> 'a'))
val board: Map[(Row, Char), String] = Map(((1, 'a'), ""))
joroKr21 commented 2 years ago

I guess the difference is that -> is an extension method and to invoke it we have to type (1, 'a') first without an expected type which widens 1: Int. It doesn't sound like something that can be fixed.

som-snytt commented 1 year ago

Similar ticket with harmonization and extensions, and effort to document status quo.

https://github.com/lampepfl/dotty/issues/13653