in reply to Raku classes: syntax question
The code appears to be a relatively simple contrived example, but the method set is such a travesty that it's a minor tragedy you've been exposed to it.
I get that it's somewhat moot now. You found/saw it, have done a bunch of work to try and understand it, and here we are, and you've done fine, so it's not a huge deal.
But the example is so bad I've concluded there's little point in me commenting much further about it, other than to make clear it's a mess as an example, even as a contrived one. Yes, you've learned from it, and posting it has gotten you helpful comments by Athanasius and steve. No, you should not stick with whatever source it came from, and I recommend you don't even bother to further your understanding of that example.
as far as I know you only write $.x when you declare the instance variable in order to generate a read accessor.
Imo that's a good thing to think, other than the tweak that has $.x is rw generates a write accessor too as steve has explained. You can and should take that on board. But other than that, please just keep on thinking what you were thinking about the only time to use code of the form $.x being in an attribute declaration.
In particular, elsewhere within a method in a class containing the attribute $!x, if you want to refer to a field x, please write one of these:
I'll close with a bit more gnarly detail about this point just in case you care to scare yourself or others.
As steve has noted, and the doc even suggests, the syntax $.x can currently be used to invoke the accessor method within other methods inside the class. Athanasius even mentions it, saying "Raku allows you to sometimes use $.x as an alias for $!x".
They're not actually aliases -- $.x calls a method, almost the same as self.x -- so please don't take the "alias" aspect literally. But I agree with Athanasius that you're best off not using $.x outside of attribute declarations.
And here's why: there are multiple outstanding bugs and quirks related to use of $.foo outside of has $.foo declarations. Such use will sometimes loudly but confusingly, or, worse, silently, do completely the wrong thing.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Raku classes: syntax question
by 7stud (Deacon) on Feb 15, 2024 at 15:24 UTC |