Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pacien opened this issue Mar 1, 2025 · 0 comments

Comments

@pacien
Copy link
Contributor

pacien commented Mar 1, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant