Raku / old-issue-tracker

Tickets from RT
https://github.com/Raku/old-issue-tracker/issues
2 stars 1 forks source link

Referring to an attribute in BUILD prevents it from getting its default value #4765

Open p6rt opened 8 years ago

p6rt commented 8 years ago

Migrated from rt.perl.org#126680 (status was 'new')

Searchable as RT126680$

p6rt commented 8 years ago

From @hoelzro

Excerpt from #perl6​:

  22​:42 hoelzro if the default logic happens after BUILD, then $!server is Str because BUILD doesn't set it up via its signature   22​:43 m​: class Person { has $.name = 'John'; has $.age = 21; submethod BUILD(​:$!name) { say $!age } }; say Person.new(​:name\)   22​:43 camelia rakudo-moar d410e3​: OUTPUT«(Any)␤Person.new(name => "Rob", age => Any)␤»   ...   22​:43 hoelzro m​: class Person { has $.name = 'John'; has $.age = 21; submethod BUILD() { say $!age } }; say Person.new(​:name\)   22​:43 camelia rakudo-moar d410e3​: OUTPUT«(Any)␤Person.new(name => "John", age => Any)␤»   ...   22​:43 Juerd Right, but that would mean that I can't use any of the defaulting logic if I want other stuff to happen at initialization, using those defaults...   22​:43 hoelzro hmm...that's interesting   22​:43 m​: class Person { has $.name = 'John'; has $.age = 21; submethod BUILD() { } }; say Person.new(​:name\)   22​:43 camelia rakudo-moar d410e3​: OUTPUT«Person.new(name => "John", age => 21)␤»   22​:43 Juerd I can only imagine that this is an extremely common use case.   ...   22​:44 hoelzro it looks like referring to any attributes in BUILD circumvents their defaulting logic, which seems...odd.   ...   22​:44 Juerd I could just add the defaults to BUILD's signature, but it's so nice to have them all in one place :(   22​:44 hoelzro Juerd​: I think that's a rakudobug