I so enjoy the low boilerplate of raku OO that I am a little surprised that I cannot shake off some integration layer boilerplate.
Here is what I have working today (somewhat golfed):
class Error {
has Real $.absolute;
method percent( $value ) {
"{ ( $!absolute / $value ).abs * 100 }%"
}
}
class Measure {
has Real $.value;
has Error $.error;
method new( :$value, :$error ) {
self.bless( :$value, error => Error.new( absolute => $error ) )
}
method error-percent { # <==== why do I need this intermediary method??
self.error.percent( $!value )
}
}
my $m = Measure.new( value => 10, error => 1 );
say $m.error-percent; #10%
This works in that...
- it separates concerns of Measure "container" and Error "element"
- $.value rightly stays in the measure and is passed "up" when needed
BUT
It does NOT reflect the fact that many error operations will need the measure value. So (how) can Raku's cool OO schema help me put a "proxy" to the $.value in the Error class
I am aiming at something like this pseudo-code:
class Error {
has Real $.absolute;
has Callable &!value-cb;
method percent {
"{ ( $!absolute / &!valuecb() ).abs * 100 }%"
}
}
class Measure {
has Real $.value;
has Error $.error;
method new( :$value, :$error ) {
self.bless( :$value, error => Error.new( absolute => $error ), value-cb => &.value() )
}
}
my $m = Measure.new( value => 10, error => 1 );
say $m.error.percent; #10% <=== note the difference, I just go straight there!
So the concept I am groping for is to create some kind of closure / function for the $.value and push it into an Error attribute on construction.
I am happy to stick with what I have, decorating most methods on the Error object with $.value if that is considered the safest route. And I apologise in advance for my lack of knowledge on the various OOP / pattern terminology for this trick.