Skip to content

Singleton constrain should infer literal type #24252

@mwisnicki

Description

@mwisnicki

Compiler version

3.7.2

Minimized code

https://scastie.scala-lang.org/hSVqiYRGQEOtddYD6PAYzQ

//> using scala 3.7.2
//> using options -Wall -Werror

//def singleton[T <: Singleton](v: T): T = v
// transparent does not help either
inline def singleton[T <: Singleton](inline v: T): T = v

enum Foo:
  case A
  case B

val e = singleton(Foo.A)
val e1: Foo.A.type = e // fail

// does not work for literal types

val s = singleton("a")
val s1: "a" = s // fail

Output

Found:    (Playground.e : Playground.Foo)
Required: (Playground.Foo.A : Playground.Foo)

Found:    (Playground.s : String)
Required: ("a" : String)

Expectation

  1. transparent inline should preserve singleton type, especially with Singleton contraint
  2. it would be useful if this worked even without inline, i.e. in presence of Singleton should infer actual singleton type

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