Hey folks,
I feel like I'm missing something simple. Maybe I've just been staring at this for too long. But I can't figure out why the last line of this doesn't print:
import hashes, math
type
Distance* = int|float
Node* = concept n
`==`(n, n) is bool
hash(n) is Hash
Graph* = concept g
distance(g, Node, Node) is Distance
type
XY* = object
x, y: int
MyGraph* = object
points: seq[seq[XY]]
proc `==`*( a, b: XY ): bool = a.x == b.x and a.y == b.y
proc hash*( xy: XY ): Hash = hash(xy.x * xy.y)
if XY is Node:
echo "XY is Node"
proc distance*( g: MyGraph, a, b: XY ): float =
sqrt( pow(float(a.x - b.x), 2) + pow(float(a.y - b.y), 2) )
if MyGraph is Graph:
echo "MyGraph is Graph"
(This code is contrived and a bit nonsensical at times; I was just trying to shrink the repro)
Thanks in advance!
If you change the distance proc to have a and b be concrete types (e.g. int), then it does print the last line.
So it seems like nesting of the concepts does not work (not sure if "nesting" is the best term for that). So when the distance(g, Node, Node) is Distance is checked to see if it compiles, the Node concept is not yet defined, so it does not compile.
I did a quick test of splitting the concepts up into separate compilation units, but that did not work either. I also tried creating a macro to define the Node concept, but no luck there either.
Note that I was testing on 0.11.2 ; I don't have devel checked out on the machine I'm on right now.
Try this please and if it still doesn't compile, file a bug report:
type
Graph* = concept g
var x: Node
distance(g, x, x) is Distance
No luck, but thanks for taking a look!
bug report filed: https://github.com/nim-lang/Nim/issues/3452