3 ms·
You really wouldn't really need to in Raku. Mainly because you can add things to the existing parser instead. (See postfix and infix below.) role Physics:
by b2gills 6y ago
You really wouldn't really need to in Raku.
Mainly because you can add things to the existing parser instead. (See postfix and infix below.)
role Physics::Datum {
has Real $.value is required;
}
role Physics::Distance {
also does Physics::Datum;
}
role Physics::Volume {
also does Physics::Datum;
}
class Physics::Volume::Gallon {
also does Physics::Volume;
}
class Physics::Distance::Mile {
also does Physics::Distance;
}
role Physics::Per[ ::N-Type, ::D-Type ] {
has N-Type $.nu is required;
has D-Type $.de is required;
# could have a submethod TWEAK to reduce the fraction
}
constant gallon = Physics::Volume::Gallon.new( value => 1 );
sub postfix:<miles> ( Real $value ){
Physics::Distance::Meter.new( :$value )
}
sub infix:<per> ( ::NT Physics::Datum $nu, ::DT Physics::Datum $de ){
Physics::Per[NT,DT].new( :$nu, :$de )
}
my $mpg = 5miles per gallon;
Which would result in the following data structure.
Physics::Per[ Physics::Distance::Mile, Physics::Volume::Gallon ].new(
nu => Physics::Distance::Mile.new( value => 5 ),
de => Physics::Volume::Gallon.new( value => 1 ),
)
I think the main reason it was written that way was it was copied from Perl.
A lot of the above would be more difficult or slower in Perl.
(This was mostly a quick throwaway example of how it could be done.)