Compilation never stops with an upper bounded type param using a union type with 3 types
See original GitHub issueCompiler version
3.1.1
Minimized code
This code compiles (although it takes a little bit, aka more than usual for so little code)
import squants.energy.{Energy, KilowattHours}
import squants.time.{Minutes, Time}
import squants.thermal.{Temperature, Kelvin}
import squants.{Quantity, UnitOfMeasure, Dimensionless, Each}
case class Price[Q <: Quantity[Q] & (Energy | Time)](amount: Double, per: UnitOfMeasure[Q])
Price(5.5, KilowattHours)
Price(5.5, Minutes)
//Price(5.5, Kelvin) // This line correctly fails to compile
Changing Price to
case class Price[Q <: Quantity[Q] & (Energy | Time | Dimensionless)](amount: Double, per: UnitOfMeasure[Q])
Causes the compiler to never stop compiling.
Output
Scastie times out after 30 seconds. Ran locally for 300 seconds then killed it.
Expectation
With a union type of 2 types it works, but with 3 types it fails. This suggests a explosion of combinations to big to handle in a reasonable time. There must be a limit to this explosion of combinations, however I was expecting it to be much higher: in the tens or even in the hundreds not 3.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Bounded Type Parameters - The Java™ Tutorials
To declare a bounded type parameter, list the type parameter's name, followed by the extends keyword, followed by its upper bound, which in...
Read more >Really Advanced Typescript Types - Tableau Engineering Blog
We're going to explore a number of advanced building blocks including custom compilation errors, surprising ways to use generics, and ...
Read more >Java generics parameter bounding to any of a range of types
No. It wouldn't make any sense unless all the types had a non-empty union type, e.g. an interface they all implemented, or a...
Read more >Enable type parameter lower-bound syntax #14520 - GitHub
Expected behavior: The type parameter A has the type Kitten as lower-bound. Actual behavior: Compilation failure. The syntax is unsupported.
Read more >Union type widening - Language Design - Scala Contributors
Currently, the compiler widens union types to a common supertype when inferencing the types of vals, defs, and type parameters.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I could find the culprit that caused the exponential explosion, and it looks like this case can be fixed, after all.
Squants defines
Quantity
with a f-bound, and since I want to have aUnitOfMeasure
I’m forced to also have an f-bound. Otherwise it would be much simpler to do:Which in an ideal world would compile since Energy, Time, and Dimensionless are all subclasses of
Quantity[Q]
however the compiler says:Type argument Q does not conform to upper bound squants.Quantity[Q]
It would be interesting to see how to solve problems the f-bounds solve without using them.