Hi Nim community! :-)
I am a new user of Nim language. It's very fun to program with.
To know Nim better, I am creating a mini GUI for fun. I am currently facing a problem that I can not solve ..
I wish I could fill a sequence of widgets with different types (Layout, Button, ..) but with a common parent (Widget).
I try the following in the documentation but I do not understand the function of the keyword "out".
When I try the documentation code I get an error that I do not understand: "The out modifier can be used only with imported types"
So, I'm blocked with the following code: (I would like to make the commented code work)
type
Widget* = object of RootObj
Layout* = ref object of Widget
widgets*: seq[ref Widget]
# widgets*: seq[ref Button]
Button* = ref object of Widget
proc newLayout(): ref Layout =
new result
result[] = new Layout
result.widgets = @[]
proc newButton(): ref Button =
new result
result[] = new Button
proc addWidget(layout: ref Layout, widget: ref Widget) =
# proc addWidget(layout: ref Layout, widget: ref Button) =
layout.widgets.add widget
var
layout0 = newLayout()
button0 = newButton()
layout0.addWidget button0
Do you know if it is possible and how to match a type compared to that of his parent?
How about this:
type
Widget* = ref object of RootObj
Layout* = ref object of Widget
widgets*: seq[Widget]
Button* = ref object of Widget
proc newLayout(): Layout =
result = new Layout
result.widgets = @[]
proc newButton(): Button =
result = new Button
proc addWidget(layout: Layout, widget: Widget) =
layout.widgets.add widget
let
layout0 = newLayout()
button0 = newButton()
layout0.addWidget button0