nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.64k stars 1.47k forks source link

[Concepts] Declarations in concept body are not considered #17420

Open konsumlamm opened 3 years ago

konsumlamm commented 3 years ago

Defining a concept without any type that matches it, leads to errors when trying to use that concept.

Example

type
  Comparable = concept
    proc compare(a, b: Self): int # so that there are no conflicts with `cmp`

proc test[T: Comparable](x, y: T) =
  echo compare(x, y)

Current Output

Error: undeclared identifier: 'compare'

Expected Output

No error.

Possible Solution

Make name resolution consider concept declarations (compare in this case).

Additional Information

$ nim -v
Nim Compiler Version 1.5.1 [Linux: amd64]
Compiled at 2021-03-18
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: de5a8265384c2f23eca744b4fc9feda9721faefd
active boot switches: -d:release
Araq commented 3 years ago

This wasn't covered by my RFC. I think this requires a different RFC, "name resolution in generics that use new-styled concepts". That said, I agree that we should do it. But notice that symbol lookup is only the first step, we could also type-check generic bodies completely.