Skip to content

Using the same type class twice collapses to a single concrete type #25165

@panno8M

Description

@panno8M

Nim Version

v2.2.4
v2.3.1 (devel #d60e021)

Description

When I try to define a generic type that takes two parameters constrained by the same type class (either a concept or a union type), compilation fails in cases that I would expect to work.

type
  Concept = concept x
    $x is string
  TypeClass = int | float

  ConceptObject[A: Concept; B: Concept] = object
  TypeClassObject[A: TypeClass; B: TypeClass] = object

# ok
var a: ConceptObject[int, int]
var b: TypeClassObject[int, int]

# error
var c: ConceptObject[int, float]
var d: TypeClassObject[int, float]

Current Output

Error: cannot instantiate ConceptObject [type declared in PATH(6, 3)]

got: <typedesc[int], typedesc[float]>
but expected: <A: Concept, B: Concept>



Error: cannot instantiate TypeClassObject [type declared in PATH(7, 3)]

got: <typedesc[int], typedesc[float]>
but expected: <A: TypeClass, B: TypeClass>

Expected Output


Known Workarounds

If I wrap one of the type classes in another concept, or define a duplicate type class, or use an inline union in the generic parameters, then it works as expected.

type
  Concept2 = concept type T
    T is Concept
  ConceptObject2[A: Concept; B: Concept2] = object

assert compiles(ConceptObject2[int, float])
type
  TypeClass2 = int | float
  TypeClassObject2[A: TypeClass; B: TypeClass2] = object

assert compiles(TypeClassObject2[int, float])
type
  TypeClassObject3[A: int|float; B: int|float] = object

assert compiles(TypeClassObject3[int, float])

Additional Information

I am not sure whether this is a compiler bug or simply a misuse on my side.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions