question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Compilation never stops with an upper bounded type param using a union type with 3 types

See original GitHub issue

Compiler 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:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
oderskycommented, Apr 7, 2022

I could find the culprit that caused the exponential explosion, and it looks like this case can be fixed, after all.

0reactions
Laseringcommented, Apr 7, 2022

Squants defines Quantity with a f-bound, and since I want to have a UnitOfMeasure I’m forced to also have an f-bound. Otherwise it would be much simpler to do:

class Price[Q <: (Energy | Time | Dimensionless)](amount: Double, per: UnitOfMeasure[Q])

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found