# New Ticket Created by Aaron Sherman # Please include the string: [perl #130020] # in the subject line of all future correspondence about this issue. # <URL: https://rt.perl.org/Ticket/Display.html?id=130020 >
The example: class Foo is Hash { has $.blah = 0 }; say Foo.new.blah.perl The output: rakudo-moar 2ef2fd: OUTPUT«Any» Expected output: 0 Reason: Hash defines its own new which bypasses the default handling in bootstrap Concerns: The code above is not obviously wrong to the user, but is guaranteed not to work, and worse, if the base class is unknown (either parameterized or via an interstitial derived class) then this code can't even be determined valid or invalid without following the context. >From IRC: [15:09] <harmil_wk> m: class Foo is Hash { has $.blah = 0 }; say Foo.new.blah.perl [15:09] <+camelia> rakudo-moar 2ef2fd: OUTPUT«Any» [15:09] <harmil_wk> I have a feeling I'm doing something I should know better than to do... [15:11] <viki> s: Hash, 'new', \() [15:11] <SourceBaby> viki, Sauce is at https://github.com/rakudo/rakudo/blob/2ef2fdb/src/core/Map.pm#L12 [15:15] <viki> m: use nqp; class Foo { has $.blah = 0; method new { self.bless: :42blaz } }.new.blah.say [15:15] <+camelia> rakudo-moar 2ef2fd: OUTPUT«0» [15:15] <viki> m: use nqp; class Foo { has $.blah = 0; method new { self.bless: :42blah } }.new.blah.say [15:15] <+camelia> rakudo-moar 2ef2fd: OUTPUT«42» [15:15] <viki> m: use nqp; class Foo { has $.blah = 0; method new { nqp::create(self) } }.new.blah.say [15:15] <+camelia> rakudo-moar 2ef2fd: OUTPUT«(Any)» [15:15] <harmil_wk> oh really... [15:16] <viki> harmil_wk: what I'm seeing is nqp::create(self) doesn't set the defaults and that's what the Map's .new method is doin' [15:16] <[Coke]> all the default logic is in the bootstrap file in rakudo, so that makes sense. [15:16] <harmil_wk> Yeah, that seems highly counterintuitive. At the very least, a warning (probably error) should result if your defaults are going to be ignored. [15:17] <viki> We can't warn about every possible usage without slowing the runtime down to a screetching halt [15:17] <viki> m: use nqp; class Foo { has $.blah; submethod BUILD (:$!blah = 42) {} }.new(:72blah).blah.say [15:17] <+camelia> rakudo-moar 2ef2fd: OUTPUT«72» [15:18] <viki> m: use nqp; class Foo { has $.blah; submethod BUILD (:$!blah = 42) {} }.new.blah.say [15:18] <+camelia> rakudo-moar 2ef2fd: OUTPUT«42» [15:18] <viki> m: class Foo is Hash { has $.blah; submethod BUILD (:$!blah = 42) {} }.new.blah.perl.say [15:18] <+camelia> rakudo-moar 2ef2fd: OUTPUT«Any» [15:19] <[Coke]> is there rakudo only code that demonstrates the issue? or do you have to (as viki did above), "use nqp" and then an nqp opcode? [15:19] <harmil_wk> My original code that derived from Hash [15:19] <harmil_wk> m: class Foo is Hash { has $.blah = 0 }; say Foo.new.blah.perl [15:19] <+camelia> rakudo-moar 2ef2fd: OUTPUT«Any» [15:19] == wisti [~brianw...@c-73-109-31-174.hsd1.wa.comcast.net] has quit [Ping timeout: 265 seconds] [15:19] <viki> lizmat: what's the answer? [15:19] <viki> I mean, answer to what [15:20] <lizmat> to your cry for help: "mother..." :-) [15:20] <viki> m: class Foo is Hash { has $.blah; method new { self.bless: %_ } }.new.blah.perl.say [15:20] <+camelia> rakudo-moar 2ef2fd: OUTPUT«Too many positionals passed; expected 1 argument but got 2 in method new at <tmp> line 1 in block <unit> at <tmp> line 1» [15:20] <viki> Ah :) [15:20] <harmil_wk> lizmat: heh [15:21] <viki> m: use nqp; class Foo { has $.blah; method new { self.bless: |:42blah, |%_} }.new(:72blah).blah.say [15:21] <+camelia> rakudo-moar 2ef2fd: OUTPUT«72» [15:21] <viki> m: use nqp; class Foo { has $.blah; method new { self.bless: |:42blah, |%_} }.new.blah.say [15:21] <+camelia> rakudo-moar 2ef2fd: OUTPUT«42» [15:22] <viki> There. I win. [15:22] <harmil_wk> My point is that "class Foo is ::($x) { has $.blah = 0 }" will only work if $x is a class that doesn't do what Hash does... that feels like a very difficult-to-detect bug from the user's side. [15:22] <[Coke]> Yup. I would say that should either work or, if it can't, throw an error asap that it can't work. [15:23] <harmil_wk> I'll rakudobug it for the record. -- Aaron Sherman, M.: P: 617-440-4332 Google Talk, Email and Google Plus: a...@ajs.com Toolsmith, developer, gamer and life-long student.