By the way, Can I create a method in the Int structure of the above example
code?
image. however, this results in a compile error.
proc addTwo(self: Int): int =
return self.num + 2
Run
It works as follows, but if you use it incorrectly for a different structure,
it will result in a runtime error.
proc addTwo(self: C): int =
return self.num + 2
echo b.addTwo() # ok
echo a.addTwo() # runtime error
Run
Although it is possible to branch conditionally by typing like TypeScript type
guard, it is annoying to write this each time.
proc addTwo(self: C): int =
if self.kind == Int:
return self.num + 2
else:
return 10000000
Run
Please tell me if there is a better way.