Skip to content

Union type too restrictive in inline match #25222

@Adam-Vandervorst

Description

@Adam-Vandervorst

Compiler version

3.8.1

Minimized code

trait Dist[T]: 
  def sample(using rng: java.util.Random): T

inline def Uniform[F <: Float | Double | Int | Long](inline low: F, inline high: F): Dist[F] = new:
  override def sample(using rng: java.util.Random): F =
    inline compiletime.erasedValue[F] match
      case _: Float => rng.nextFloat(low, high)
      case _: Double => rng.nextDouble(low, high)
      case _: Int => rng.nextInt(low, high)
      case _: Long => rng.nextLong(low, high)

Output

        case _: Float => rng.nextFloat(): F
                         ^^^^^^^^^^^^^^^
Found:    Float
Required: F

where:    F is a type in method Uniform with bounds <: Float | Double | Int | Long

 Tree:

 rng.nextFloat()

 I tried to show that
   Float
 conforms to
   F
 but none of the attempts shown below succeeded:

   ==> Float  <:  F
     ==> Float  <:  Nothing in frozen constraint  = false

 The tests were made under the empty constraint

Expectation

I expect the constraint not to be empty, specifically for the type match to constrain F to Float.

Partial workaround

inline (low, high) match
        case (low: Float, high: Float) => rng.nextFloat(low, high)

does work for constraining the inputs, but this still fails on the return type without asInstanceOf

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions