This channel is intended for people just starting with the Raku Programming Language (raku.org). Logs are available at irclogs.raku.org/raku-beginner/live.html Set by lizmat on 8 June 2022. |
|||
01:44
Guest20 joined
|
|||
Guest20 | Hi everyone. I'm pretty new to Raku and I'm trying to figure out how to set a class attribute based on another attribute in the same class, upon creation of a class instance. | 01:49 | |
So for instance if I had an attribute 'has @.column = [1,2,3,4,5];' and 'has $.column-length is built(False)', how would I get $.column-length to be set to the length of the @.column upon the creation of an instance of the class? | |||
I tried method '!set-length() { $!length = 5 }', but that requires the method to be called after the instance is created. | |||
also sorry for the lack of code formatting, not sure how to do it in IRC. | |||
Ah nevermind, I figured it out. | 02:10 | ||
I just used the TWEAK submethod. I'm coming from Rust so a lot of basic OOP stuff is new to me. | |||
04:09
elcaro_ joined
|
|||
elcaro_ | Guest20: You can refer to previously defined attributes, eg. `has @.column; has $.elems = @!column.elems` | 04:12 | |
However in this case, `elems` could be set during .new which would over-ride your default... so you would probably want `has $!elems = @!column.elems`, and then add `method elems { $!elems }`. | 04:13 | ||
I assume this is a contrived example, because for this specific answer, you probably wouldn't use an attribute at all, you'd just create a `method elems { @!column.elems }` | 04:14 | ||
04:15
elcaro_ is now known as elcaro
|
|||
Guest20 | elcaro thanks for the advice! Yes it is a contrived example. In reality I have an attribute with many columns and want to make sure they are all the same length, and then put that length into the elems attribute. But good to know I can refer to previously defined attributes. TWEAK works but there might be a cleaner way using your method. I'll play | 04:55 | |
around with it. | |||
05:12
Guest20 left
|
|||
librasteve | Guest20: welcome to the fun house. rust is unforgiving, raku is [mostly] easy in comparison. Generally at beginner level, you should be able to avoid the OO traits such as is built - if you find yourself needing more than the occasional TWEAK method, then it’s a hint that you are bending OO a little out of shape for your ends. of course in raku all kinds of bending can be done … point is that the | 06:53 | |
defaults are well chosen to gently nudge you the “right” way | |||
i reckon if you can get your head around the docs OO section docs.raku.org/language/objects (not including Meta stuff) then that is already a pretty deep set of skills in raku and in OO in general … other languages have nice OO syntax (Ruby) or power tools like mutli inheritance and role composition (C++) but raku is the only one I know that gives you all the power AND a very natural and easy to | 07:00 | ||
read syntax | |||
07:10
stanrifkin joined
08:09
dakkar joined
16:31
dakkar left
18:16
lizmat left
18:22
lizmat joined
20:48
habere-et-disper joined
22:12
habere-et-disper left
22:26
habere-et-disper joined
22:35
habere-et-disper left
22:46
stanrifkin left
|