Hello,
I am quite new to nim language and started to develop a graphic app in web (using canvas / webgl).
I know nim is not OOP based but I still tried to achieve OOP using 'method'. But when I use two arguments using 'method' it does not work..
Am I wrong ? Or would there be better ways to implement that in nim ?
Thanks
type
Drawable* = ref object of RootObj
Sprite* = ref object of Drawable
Graphics* = ref object of Drawable
Renderer* = ref object of RootObj
Renderer2D* = ref object of Renderer
RendererWebGL* = ref object of Renderer
# OOP works here with one argument
method printType*(d: Drawable) {.base.} =
echo "Drawable"
method printType*(s: Sprite) =
echo "Sprite"
method printType*(g: Graphics) =
echo "Graphics"
# When using multiple arguments OOP is not working
method render*(r: Renderer, d: Drawable) {.base.} = discard
method render*(r: Renderer2D, d: Drawable) = discard
method render*(r: Renderer2D, g: Sprite) =
# Do not go here...
echo "Renderer2D Sprite"
method render*(r: Renderer2D, g: Graphics) {.base.} =
# Do not go here...
echo "Renderer2D Graphics"
var arr: seq[Drawable]
let sprite1 = Sprite()
let graphics1 = Graphics()
arr.add(sprite1)
arr.add(graphics1)
var renderer: Renderer
renderer = Renderer2D()
for e in arr:
e.printType()
renderer.render(e)
See the Nin manual, section
https://nim-lang.org/docs/manual.html#multiminusmethods
Note: Starting from Nim 0.20, to use multi-methods one must explicitly pass --multimethods:on when compiling.