The file below compiles and runs.
But, when I chop it up into separate files it's a falure: Error: type mismatch: got 'Circle' for 'c1' but expected 'Shape'
What works then is merge render.nim and scene.nim, add import shapetype and include circle. Fine with just circle, but not with many more shapes.
Why does Shape not 'propagate'? Do I understand it wrong? Do I implement it wrong?
import vmath
#shapetype.nim
type
ShapeId* = enum
siCircle
Shape* = concept shape
shape is object
shape.id is ShapeId
# ---%<---%<---%<--- circle.nim
type
Circle* = object
id: ShapeId = siCircle
center*: Vec2
radius*: float
proc circle*(center:Vec2, radius: float):Circle =
Circle(center:center, radius:radius)
proc intersect*(c: Circle, point: Vec2): bool =
let dist = length(point - c.center)
return dist <= c.radius
proc distance*(c: Circle, point: Vec2): float =
return abs(length(point - c.center) - c.radius)
# ---%<---%<---%<--- render.nim
proc render*(s:Shape, p:Vec2)=
echo s.intersect(p)
echo s.distance(p)
# ---%<---%<---%<--- scene.nim
let c1 = circle(vec2(0.0,0.0),2.2)
let s1 = Shape(c1) #to be added to R-tree.
render(s1, vec2(3.3,4.4) )
from the docs: The concept matches if: all expressions within the body can be compiled for the tested type.
After reordering the code it works, all types in one file, enum first than the circle etc and finally the Shape.
or:
type
ShapeId* = enum
siCircle, siEllipse
import circle
import rectangle
type
Shape* = concept shape
shape is object
shape.id is ShapeId