I try to make a control type in https://khchen.github.io/wNim/, so I modified the examples coming with wNim.
The control will work like this: after click any of the 2 buttons on the frame, the frame will be closed, and a number will be returned to tell which button has been clicked.
However, the code will not be compiled due to
subclassing.nim(16, 13) Error: implicit object field construction requires a .partial object, but got wMyFrame:ObjectType
On the other hand, if I only write bt1 and bt2 without self. before them, I get
subclassing.nim(27, 5) Error: undeclared identifier: 'bt1'
I think I must misunderstand the type in nim, please correct me, thanks
{.this: self.}
import wNim
type
wMyFrame = ref object of wFrame
proc final(self: wMyFrame) =
wFrame(self).final()
proc init(self: wMyFrame, title: string) =
wFrame(self).init(title=title, size=(350, 200))
center()
var
panel = Panel(self)
bt1 = Button(panel, label="1", pos=(0, 0))
bt2 = Button(panel, label="2", pos=(100, 0))
self.wEvent_Close do (event: wEvent):
let dlg = MessageDialog(self, "Do you really want to close this application?",
"Confirm Exit", wOkCancel or wIconQuestion)
if dlg.showModal() != wIdOk:
event.veto()
proc show(self: var wMyFrame): int {.discardable.} =
bt1.wEvent_Button do (event: wEvent):
self.endModal()
self.close()
return 1
bt2.wEvent_Button do (event: wEvent):
self.endModal()
self.close()
return 2
proc MyFrame(title: string): wMyFrame {.inline.} =
new(result, final)
result.init(title)
when isMainModule:
let app = App()
let frame = MyFrame("Hello World")
var
res = frame.show()
app.mainLoop()
Try this.
{.this: self.}
import wNim
type
wMyFrame = ref object of wFrame
mWhichButton: int
panel: wPanel
bt1: wButton
bt2: wButton
proc final(self: wMyFrame) =
wFrame(self).final()
proc init(self: wMyFrame, title: string) =
wFrame(self).init(title=title, size=(350, 200))
center()
panel = Panel(self)
bt1 = Button(panel, label="1", pos=(0, 0))
bt2 = Button(panel, label="2", pos=(100, 0))
self.wEvent_Close do (event: wEvent):
self.mWhichButton = 0
let dlg = MessageDialog(self, "Do you really want to close this application?",
"Confirm Exit", wOkCancel or wIconQuestion)
if dlg.showModal() != wIdOk:
event.veto()
bt1.wEvent_Button do (event: wEvent):
self.close()
self.mWhichButton = 1
bt2.wEvent_Button do (event: wEvent):
self.close()
self.mWhichButton = 2
proc whichButton(self: wMyFrame): int {.discardable.} = self.mWhichButton
proc MyFrame(title: string): wMyFrame {.inline.} =
new(result, final)
result.init(title)
when isMainModule:
let app = App()
let frame = MyFrame("Hello World")
frame.show()
app.mainLoop()
echo frame.whichButton()
Aha, thanks. I have just found https://github.com/jyapayne/nim-extensions and rewrite the code. Now it seems that there is no much difference between nim-extensions version and your type version, but nim-extensions version can be understood easily for me since I have some Python background.
BTW, it seems that there is a direct way to declare static class member as https://stackoverflow.com/questions/68645/are-static-class-variables-possible said
import wNim
import extensions/oop
class MyFrame of RootObj:
var
title: string
frame: wFrame
panel: wPanel
bt1, bt2: wButton
clicked: int
method init*(title: string) {.base.}=
self.frame = Frame(title=title)
self.panel = Panel(self.frame)
self.bt1 = Button(self.panel, label="1", pos=(0, 0))
self.bt2 = Button(self.panel, label="2", pos=(100, 0))
self.frame.wEvent_Close do (event: wEvent):
event.veto()
self.bt1.wEvent_Button do (event: wEvent):
self.frame.endModal()
self.frame.close()
self.clicked = 1
self.bt2.wEvent_Button do (event: wEvent):
self.frame.endModal()
self.frame.close()
self.clicked = 2
method show: int {.base, discardable.} =
self.frame.showModal()
return self.clicked
when isMainModule:
let app = App()
let
frame = Frame(title="main", style=wDefaultFrameStyle)
bt = Button(frame, label = "click")
bt.wEvent_Button do (event: wEvent):
let
myframe = new MyFrame(title: "Hello World")
res = myframe.show()
echo res
frame.center()
frame.show()
app.mainLoop()
I develop wNim without any OOP-extensions. However, it's is truely OOP in nim's way. I am not opposed to any OOP-extensions for nim. But It's better for anyone to understand what the extensions does in the backend.
BTW, your first code is Inheritance (wMyFrame is a wFrame), second code is Composition (MyFrame has a wFrame).