I am trying to do some OOP with Perl6 and am having a little trouble with roles. I am trying to use them in a similar way to a Java interface, where I would just have method signatures that must be implemented by any class that does the role. I am using stubbed methods with typed parameters and return.
I am noticing that the type signatures are not being enforced though, only the name of the method.
Example script:
#!/usr/bin/env perl6
use v6;
role MyRole {
method intAdder( Int $a, Int $b --> Int ) { ... }
}
# this does the role and the method signature matches
class MyClass1 does MyRole {
method intAdder( Int $a, Int $b --> Int ) { return $a+$b }
}
# this does the role and the method signature does NOT match
# why is this allowed?
class MyClass2 does MyRole {
method intAdder( Str $a --> Str ) { return "Hello, $a." }
}
# this does the role and the method name does not match and gives an error as expected:
# Method 'intAdder' must be implemented by MyClass3 because it is required by roles: MyRole.
#
# class MyClass3 does MyRole {
# method adder( Int $a, Int $b --> Int ) { return $a+$b }
# }
sub MAIN() {
my $object1 = MyClass1.new;
my $object2 = MyClass2.new;
say $object1.intAdder: 40, 2;
say $object2.intAdder: 'world';
}
# output:
# 42
# Hello, world.
I have read through the Object orientation page in the official docs and can't find a way to do what I want... I am also trying to apply a Java way of thinking about OOP and typing and maybe there is a different, more Perl6ish way to do what I want...