Skip Menu |
Report information
Id: 133246
Status: rejected
Priority: 0/
Queue: perl6

Owner: Nobody
Requestors: richard.hogaboom [at] gmail.com
Cc:
AdminCc:

Severity: (no value)
Tag: (no value)
Platform: (no value)
Patch Status: (no value)
VM: (no value)



To: rakudobug [...] perl.org
Subject: Possible bug in Perl 6 OO
From: Richard Hogaboom <richard.hogaboom [...] gmail.com>
Date: Mon, 4 Jun 2018 10:31:23 -0400
Download (untitled) / with headers
text/plain 398b
Attached is an executable file that demos a possible Perl 6 OO bug if attributes and method names are the same.  This includes some compile time errors and an infinite loop. -- Richard A Hogaboom 16 Alprilla Farm Road Hopkinton, MA 01748-1922 richard.hogaboom@gmail.com www.linkedin.com/in/richardhogaboom/ https://plus.google.com/+RichardHogaboom https://github.com/rahogaboom M508-330-3775
Download perl6bug.p6
text/plain 2.2k

Message body is not shown because sender requested not to inline it.

RT-Send-CC: perl6-compiler [...] perl.org
Download (untitled) / with headers
text/plain 1.9k
On Mon, 04 Jun 2018 07:31:40 -0700, richard.hogaboom@gmail.com wrote: Show quoted text
> Attached is an executable file that demos a possible Perl 6 OO bug if > attributes and method names are the same.  This includes some compile > time errors and an infinite loop. >
Thanks for the report, but there's no bug here. The `has $.foo` attribute syntax is just a shorthand for writing `has $!foo; method foo { $!foo }`. In other words, the compiler auto-generated an accessor method for you. When you use `$.foo` syntax inside a method, it's a shorthand for `self.foo`; in other words, it's a method call. If you define a method with the same name as the attribute, then it signals to the compiler that you chose to take care of accessor method yourself. Also, to return a writeable container, you need to either use `return-rw` instead of plain `return` when using explicit return, or apply the `is rw` trait to the method, when using implicit return: method var1() is rw { return $!var } method var2() { return-rw $!var } With that knowledge, you can see why the failing examples fail: * EXAMPLE 3: You defined a method with the same name as attribute, so it'll function as the accessor. The body of that method is empty, so it returns `Nil` and hence the error about trying to assign to `Nil` * EXAMPLE 4: Again, you defined your own accessor, but now you're also using `$.var` syntax inside, so the method infiniloops calling itself over and over. Hence the hang. * EXAMPLE 5: You've used `return` instead of `return-rw`, so only a readonly value is returned instead of a writable container. Hence the error Show quoted text
> but shouldn't there be some type of prohibition/warning?
No, as it's perfectly fine to declare your own accessors. Of course, writing `has $.foo` instead of `has $!foo` is pointless, in that case, but it serves as a hint to the programmer that it's a public attribute, so I don't think warning about that is appropriate.
RT-Send-CC: perl6-compiler [...] perl.org
Download (untitled) / with headers
text/plain 299b
On Sat, 09 Jun 2018 05:33:04 -0700, cpan@zoffix.com wrote: Show quoted text
> when > using implicit return: > > > method var1() is rw { return $!var } > method var2() { return-rw $!var }
Correction, that should be: method var1() is rw { $!var } method var2() { return-rw $!var }


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

For issues related to this RT instance (aka "perlbug"), please contact perlbug-admin at perl.org