Imagine every function of instance of its own class with every function argument as class field.
sub foo {
has $x => isa => <Constraint>, default => 1;
has $y => isa => <Constraint>, default => 2;
return $x + $y;
}
Treat function reference as a role
sub Parent::foo {
has $x => default => 1;
has $y => default => 2;
}
sub Child::foo {
does \& Parent::foo;
return 2 * $x + $y;
}
my $foo = sub { ... };
my $bar = sub { does $foo; ... }
override bar {
return 2 * $x + $y;
}
package Parent {
sub foo {
returns Number::Even;
}
}
package Child {
override foo {
returns Number::odd;
# every return will
}
}
sub foo {
has $x => required => 1;
has $y => required => sub { $x % 2 };
}
sub volume {
has $x => required => sub { ! exists $area };
has $y => required => sub { ! exists $area };
has $z => required => 1;
has $area => default => sub { $x * $y };
return $area * $z;
}
using modified Object::Pad example
class Point {
has $x = 0;
has $y = 0;
method move {
has $dX;
has $dY;
$x += $dX;
$y += $dY;
}
}
continuation of previous example
{
method move {
has $dX =>
required => sub { exists $dY && ! exists $radius },
default => sub { $distance * cos $radius },
;
has $dY =>
required => sub { exists $dX && ! exists $radius },
default => sub { $distance * sin $radius },
;
has $radius => required => sub { ! exists $dX || ! exists $dY };
has $distance => required => sub { exists $radius };
$x += $dX;
$y += $dY;
}
}
$obj->move (1, 1);
$obj->move (radius => 0.707, distance => 0.707);
$obj->move (dX => 1, dY => 1);
$obj->move (1, dX => 1);
$obj->move (1, dY => 1);