🦋 Welcome to the MAIN() IRC channel of the Raku Programming Language (raku.org). Log available at irclogs.raku.org/raku/live.html . If you're a beginner, you can also check out the #raku-beginner channel!
Set by lizmat on 6 September 2022.
tbrowder howdy, question about a class object generated by a single factory class: can the new class have a reference to the factory class and call a method on it? i want to avoid duplicate instantiation of a shared object (a "heavy" font object). thanks 11:18
tellable6 2024-07-20T00:37:26Z #raku <wayland76> tbrowder Oh! That makes sense. Thanks :)
librasteve tbrowder: I guess your "single factory class" implemented as a singleton (ie instead of just class methods, there is a [single] instance that can run the method gimme-new-thing - in which case then you can have class new-thing {has $.factory; ...} and set the $factory attr to the self of the factory instance. If new-think is a class object, then maybe it needs to have a my $factory and you have to set that via a 12:15
class method on new-thing...
vendethiel m: sub f($x) { return my class :: { method f { $x } } }; my $a = f(1).new; my $b = f(2).new; say $a.f; say $b.f 14:03
evalable6 2
2
Raku eval 2 2
tbrowder librasteve: thanks for the info. i looked at the singleton thing for the factory but i don't think i need it because my scripts using it only use one instance. the hatched objects are all effectively siblings and don't commmunicate with each other. 14:39
ugexe have the factory add a runtime role that adds the method from the factory class 14:43
tbrowder given that do you see any problems with yr suggestion? it sounds like it should work fine. (i have to go soon but will try it later today.) 14:44
ugexe class MyFactory { my role DoFactoryMethod { method do-it { $.factory-method } }; method factory-method { 42 }; method new { my $factory-created = MyOtherClass.new but DoFactoryMethod } } 14:45
something like that
tbrowder ugexe: i didn't see yr reply before i answered librasteve
ugexe er the code i posted isn't correct so ignore that part of my response 14:46
antononcube @tbrowder class FontFactory { has $.font; method get-font { return $!font //= self.create-font; } method create-font { # Simulate heavy font creation note '⎡DOING .create-font.⎦'; return "Heavy Font Object"; } } class TextObject { has FontFactory $.factory; method new-text { my $font = $!factory.get-font; say "Using font: $font"; 14:48
} } my $factory = FontFactory.new; my $text1 = TextObject.new(factory => $factory); my $text2 = TextObject.new(factory => $factory); $text1.new-text; # Using font: Heavy Font Object $text2.new-text; # Using font: Heavy Font Object
tbrowder i need to show a gist of how i want it to act. basically the factory has as an attribute a list of distinct font objects that its offspring should use as is--just interrogating the list. 14:49
antononcube When run the code above should give: ⎡DOING .create-font.⎦ Using font: Heavy Font Object Using font: Heavy Font Object
@tbrowder Hmm... I made/posted my code befor seeing your last clarification... 14:50
ugexe why not pass the list into the child objects 14:51
that would make testing the child objects easier as well
tbrowder hm, i forgot one important thing: the chidren may need a new font object that factory hasn't creeated. maybe use a global hash 14:53
ugexe the factory should really just be creating the children 14:55
tbrowder or a separate global singleton that just produces and holds requested font objects
and as you said factory just creates children 14:57
hm, thoughts to ponder on my road trip (when i'm not driving ;-D) 14:59
aha! i lied, each child only gets one font object, so the factory ensures they get a copy of an existing one (slapping forehead) 15:02
antononcube weekly: youtu.be/LmLk9UB4_Ak 18:23
notable6 antononcube, Noted! (weekly)