Skip to content

new-styled concepts: type mismatch for function implemented for other generic concept #24742

@pacien

Description

@pacien

Nim Version

Nim Compiler Version 2.2.2 [Linux: amd64]
Linux 6.6.56, NixOS 24.11

Description

I'm attempting to define a concept using a function implemented for another generic concept.
The following example compiles with old-styled concepts, but fails with new-styled concepts:

test.nim:

type Indexable[T] = concept
  proc `[]`(t: Self, i: int): T
  proc len(t: Self): int

iterator items[T](t: Indexable[T]): T =
  for i in 0 ..< t.len:
    yield t[i]


# With new-styled concepts: type mismatch
type Enumerable[T] = concept
  iterator items(t: Self): T

# With old-styled concepts: works
# type Enumerable[T] = concept c
#   items(c) is T

proc echoAll[T](t: Enumerable[T]) =
  for item in t:
    echo item


type DummyIndexable[T] = distinct seq[T]

proc `[]`[T](t: DummyIndexable[T], i: int): T =
  seq[T](t)[i]

proc len[T](t: DummyIndexable[T]): int =
  seq[T](t).len


let dummyIndexable = DummyIndexable(@[1, 2])
echoAll(dummyIndexable)

Current Output

test.nim(33, 8) Error: type mismatch
Expression: echoAll(dummyIndexable)
  [1] dummyIndexable: DummyIndexable[system.int]

Expected one of (first mismatch at [position]):
[1] proc echoAll[T](t: Enumerable[T])

Expected Output

1
2

Known Workarounds

Using old-styled concepts (as commented in the example snippet) works.

Additional Information

No response

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