Skip to content

Union singleton types weirdness with isInstanceOf #12796

Closed
@Swoorup

Description

@Swoorup

isInstanceOf has difference when run in compile time vs runtime. Here is an sbt example where a valid subtype isn't detected, when passed as method arg.

Compiler version

3.0.1-RC1

Minimized code

type Timeframe = "1m" | "2m" | "1H"

@main def main(input: String) = 
  val isTimeframe = input.isInstanceOf[Timeframe]
  val oneM: String = "1m"
  println(s"input is of Timeframe: $isTimeframe")
  println(s"1m is of Timeframe: ${oneM.isInstanceOf[Timeframe]}")

Output

Running with 1m which is a valid Timeframe type.

sbt:scala3-simple> run 1m
[info] running main 1m
input is of Timeframe: false
1m is of Timeframe: true

Expectation

sbt:scala3-simple> run 1m
[info] running main 1m
input is of Timeframe: true
1m is of Timeframe: true

Original motivation for doing this was to check if an input from untrusted source is of the specified type.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions