-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
avoid having to redefine 'new' constructors when overloading initialize #22
base: master
Are you sure you want to change the base?
Conversation
Is this something similar to what Vala does? |
inspired by Vala and in regards to Vala's construct block, yes, when initialization code should always be run, regardless of the constructor invoked, use the |
I'm not sure about the name, Crystal inherited the I'm not sure I get the point of the macro, just doing Finally I wonder if you thought about just calling it from the generated |
The naming could be any name. Just rolled with what I was familiar with. As for calling from initialize. |
I'm probably not seeing something, so bear with me please, but why don't we want the hook to run from classes instantiated via cast? |
wouldn't the constructor code be being called too much? say i have a container with children of MyType, the children have thier states changed by the user, I then iter the children of the container, |
Maybe cast is named wrong, it's not meant to replace crystal's as, it's for converting a more generic instance returned by a binding to a more specific binding type. |
Heres an example that demonstrates why the hook in initialize woud not best. require "../src/gtk/autorun"
class MyWidget < Gtk::Button
def initialize(ptr)
super
setup
end
def setup
self.label = "Default Label"
end
def self.new
super
end
end
w = Gtk::Window.new
w.add v=Gtk::VBox.new(false,0)
3.times do |i|
v.pack_start(mw=MyWidget.new, false, false, 1)
mw.label = "button#{i}"
mw.on_clicked do
v.foreach(->(c : LibGtk::Widget*, d : Void*) {p MyWidget.new(c.as(LibGtk::Button*)); nil}, nil)
end
end
w.show_all By default |
Mh, otoh. wouldn't we want it to run if you fetch it from say |
|
This now allows constructor and cast calls to fire the instantiated code at the proper time. The hook in initialize remains troublesome, However in currently the chain from a constructor call is...
|
…space like library
class MyType < SuperType
def instantiate
## do stuff
# ...
## used to return this exact wrapper
keep_wrapper
end
end
container.add mt=MyType.new
container.foreach do |c|
_mt = c.as(MyType)
end
mt.on_some_event do |mt_as_base|
_mt = mt_as_base.as(MyType)
_mt == mt
end |
an after initialize hook, not applicable to
cast
the macro makes it look less artificial :D (crystal should have this built in :/)