-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
super inside initialize #4764
Comments
If you use class Parent
property a : Int32
def initialize(@a)
end
def initialize
@a = 1
end
end
class Child < Parent
def initialize
super
end
def initialize(a)
super(a)
end
end
puts Child.new.a # => 1 Minimal code with this issue: class A
def initialize(@a = 1)
end
end
class B < A
def initialize
super
end
def initialize(@a)
end
end
B.new Outputs:
|
This looks like a genuine bug to me. |
maxfierke
added a commit
to maxfierke/crystal
that referenced
this issue
Aug 20, 2017
If initialize calls super and declares instance var that has already been typed as non-nilable by an ancestor, it'll be non-nilable in the child class. Fixes crystal-lang#4764
RX14
pushed a commit
that referenced
this issue
Aug 31, 2017
If initialize calls super and declares instance var that has already been typed as non-nilable by an ancestor, it'll be non-nilable in the child class. Fixes #4764
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In #4762 @RX14 said its possible to use
super
from the child constructor.Using the same example code as in the other issue, but adding a constructor that only calls
super
doesn't work.https://play.crystal-lang.org/#/r/2gb3
Outputs:
But the fun part starts now. If I remove the second constructor from the class
Child
it magically works.https://play.crystal-lang.org/#/r/2gb4
Now crystal detects that the
Child
classesinitialize
method calls super which callsParent
classesinitialize
method that initializes instance variable@a
of Parent.The text was updated successfully, but these errors were encountered: