Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
Unable to make a const instance of an inherited, generic object. #11268
Compilation fails when it includes a
type Point1[T] = object of RootObj x:T Point2[T] = object of Point1[T] y:T proc main = var a = Point2[int](x:1, y:2) echo a let b = Point2[int](x:1, y:2) echo b const c = Point2[int](x:1, y:2) echo c when isMainModule: main()
Updated my toolchain using choosenim approximately 6 hours ago.
I think it brings extra complexity with little benefits.
type Point1 = object of RootObj x: int Point2 = object of Point1 y: int PointString = object of Point1 s: string var a: seq[Point1] a.add Point1(x: 1) a.add Point2(x: 2, y: 2) a.add PointString(x: 3, s: "s") echo a
Whether or not inheritance is appropriate for this snippet, I think it's reasonable to assume there are cases where non-ref inheritance is useful.
I don't use the "generic inherited point" pattern in code; it's an example I use to explore what's possible given the language's features.
@mratsim I know what non ref inheritance means. Nobody tries to do that here.
I was thinking that the combination of generics and inheritance at compile time causes the problems here, but aparently that is not the cause of this problem. The snippet refuses to work even without generics:
type Point1 = object of RootObj x: int Point2 = object of Point1 y: int proc main = var a = Point2(x:1, y:2) echo a let b = Point2(x:1, y:2) echo b const c = Point2(x:1, y:2) echo c static: let d = Point2(x:1, y:2) echo d when isMainModule: main()
@linkmonitor it's not outright impossible to have const objects with inheritance and case objects in them. But it is not implemented in the VM. So if the demand to support this functionality for Nim raises and people actually want to have const cast objects with inheritance, then it might be possible to implement it. But right now it is more important to fix issues with existing features and have proper error messages on features that just don't work well.
But really, normally it is totally sufficient to have normals objects as const values.