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. |
|||
00:39
QhpAptyj9hj0RQwM joined
01:37
QhpAptyj9hj0RQwM left
03:57
Ebudae is now known as Heptite
05:43
Heptite left
06:47
WickedTortoise joined
06:50
CodeTortoise left
06:52
WickedTortoise left
08:40
equinox joined
|
|||
equinox | hi, say I have a method named "g" in a class, and depending on an instance attribute upon initialization, I would like to assign this method to either a method "&f_1" or "&f_2"; how can I do it? | 08:48 | |
so, e.g., for an instance `A.new(val => 0)`, the call `.g` on it will route to `.f1`, and otherwise to `.f2` | |||
so that this is not an XY problem, the case is that the class does the Iterator role, and an instance's `pull-one` method will be depending on the passed attribute | 08:49 | ||
i thought of having the if-else check within the `pull-one` and route in there, but maybe there's another way | 08:50 | ||
as that would do the check every time the method is called, although for a given instance, what to call is actually fixed upon its initialization | 08:51 | ||
jaguart | you could return iterator-f1 or iterator-f2 in your iterator method... | 09:06 | |
or just have your method call an internal sub, and assign to this sub in your TWEAK | 09:07 | ||
equinox | thanks; so your first suggestion is to make separate classes? | 09:09 | |
and for the second one: can you please show an example for it? because i don't know how to dynamically assign a method to an instance | 09:10 | ||
figured to the class with `^add_method` i think, but not sure about the instances | |||
jaguart | no - if you are Iterable are you not returning an iterator? | 09:11 | |
equinox | it's a class actually, i'm not really returning anything in that sense | 09:12 | |
`class A does Iterator { ... }` | |||
jaguart | in any case there is an easier way... give me a sec | 09:13 | |
m: class C { has $.x; has &!a; sub a1 { 'a1' }; sub a2 { 'a2' }; method b { &!a() }; submethod TWEAK { &!a = $!x %% 2 ?? &a1 !! &a2; } }; say C.new(:x(1)).b; say C.new(:x(2)).b; | 09:15 | ||
camelia | a2 a1 |
||
jaguart | &!a - a private sub-reference which you call from your method. in your TWEAK you set this to either &a1 or &a2 | 09:16 | |
equinox | thanks! so `a1` and `a2` need to access instance attribute as well, e.g., `$!x` in your case. I did `self.^find_method("a2").assuming(self)` | 09:21 | |
how would you do that? | |||
jaguart | try: method b { &!a( self ) } | 09:22 | |
this would pass self into whichever of a1 or a2 was called | |||
so maybe even switch a1 and a2 to private methods... | 09:23 | ||
equinox | okay so i first converted a1 and a2 to methods | ||
but then it says "undeclared routines a1, a2" | |||
m: class C { has $.x; has &!a; method a1 { "From a1: $!x" }; method a2 { "From a2 $!x" }; method b { &!a(self) }; submethod TWEAK { &!a = $!x %% 2 ?? &a1 !! &a2 } }; say C.new(:x(1)).b; say C.new(:x(2)).b; | 09:24 | ||
camelia | ===SORRY!=== Error while compiling <tmp> Undeclared routines: a1 used at line 1 a2 used at line 1 |
||
Nemokosch | &a1 doesn't exist indeed, nor does &a2 | 09:26 | |
jaguart | equinox: on a separate thought - have you considered using a multi method instead and getting raku to choose the right one at runtime? | 09:28 | |
you could use subs and pass in self - but then you are restricted to using the public interface on your instance | |||
what is the selection / differentiation / reason to have two paths? | 09:29 | ||
like here: docs.raku.org/language/functions#m...definition | 09:33 | ||
equinox | oh yeah i didn't think about multi dispatch! i will either do that or the assuming thing thanks a lot | ||
as for the reason... | |||
think of a "clipper" iterator: you give it an iterable, a lower bound, an upper bound | 09:34 | ||
but lower and upper bound values are optional; except you need to supply at least one of them | |||
then the iterator will look at the incoming stream's values: if a value is out of bounds, clip it and send back as such | |||
otherwise, send as is | |||
now there are 3 cases: lower supplied, upper supplied, lower & upper supplied | 09:35 | ||
the pull-one method is not really complicated per se | |||
but the fact that lower and/or upper lead me to different subs for each 3 cases | 09:36 | ||
jaguart | what do you mean by "clip it"? | ||
equinox | you gave 3 as the lower bound, 8 as the upper bound: if an incoming value is not in [3, 8], it will be clipped: if it is, e.g., 12, it will be 8; if it is, e.g., -4, it will be 3 | 09:37 | |
but you may or may not supply both lower and upper at the same time | 09:38 | ||
only lower maybe, for example | |||
then the code inside pull-one has nothing to do with an upper bound | |||
i though instead of if-else chains for checking the existence of bounds and acting accordingly in a single pull-one method, I'd rather write 3 separate methods for each case's clipping, and route accordingly | 09:39 | ||
sorry for the rambling but yeah that's the case if it makes sense | |||
jaguart | sure - but is that really three methods? - return $min ~~ Int && $val < $min ?? $min !! $max ~~ Int && $val > $max ?? $max !! $val; | 09:42 | |
equinox | no it isn't yes; but that would check things every time the method is called | 09:43 | |
could be premature optimization on my side | |||
but from another angle, i wanted to ask how to dynamically assign a method to an instance, let's say :p | 09:44 | ||
:d not :\p | |||
jaguart | well the tests will short circuit - and the only extra tests are whether $min and $max are defined | ||
equinox | i hate that emoji sorry | ||
Nemokosch | 😄 | ||
equinox | i see thanks jaguart a lot, i need to leave soon | 09:45 | |
jaguart | :) | ||
Nemokosch | If you are really worried about those checks, you could just store the result in a private attribute 😛 But tbh it seems to me that you're gonna need to perform some checks either way | 09:46 | |
09:57
equinox left
11:04
QhpAptyj9hj0RQwM joined,
QhpAptyj9hj0RQwM left
11:36
equinox joined
12:49
equinox left
13:20
equinox joined
|
|||
Luolong | What is the easiest way to surround current context in double- or single quotes? | 14:06 | |
What is the easiest way to surround current topic value in double- or single quotes? | 14:09 | ||
14:12
jgaz joined
14:29
Kaipei left
|
|||
lizmat | "$_" ? | 14:29 | |
15:04
WickedTortoise joined
15:10
jgaz left
15:12
Heptite joined
15:47
WickedTortoise left
15:52
Kaipei joined
17:04
ab5tract joined
|
|||
Nemokosch | is there something like a final/sealed class in Raku? | 17:08 | |
17:19
equinox left
20:34
ab5tract left,
ab5tract joined
20:59
jgaz joined
22:07
jgaz left
22:32
jgaz joined
|
|||
jaguart | I'm repeating a question here cos it got lost in a discussion | 23:42 | |
what are the differences between: use v6; use v6+; use v6.d+ - and which should I be using in my classes and modules? |