是否在Perl6的角色中强制使用存根方法的类型签名?

ryn*_*n1x 11 rakudo perl6

我试图用Perl6做一些OOP并且在角色方面遇到一些麻烦.我试图以类似于Java接口的方式使用它们,在那里我只有方法签名,必须由任何扮演角色的类实现.我使用带有类型参数的stubbed方法并返回.

我注意到类型签名没有被强制执行,只有方法的名称.

示例脚本:

#!/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.
Run Code Online (Sandbox Code Playgroud)

我已经阅读了官方文档中的面向对象页面,无法找到一种方法来做我想要的...我也尝试应用Java思考OOP和打字的方式,也许有一个不同的,更多Perl6ish做我想要的方式......

rai*_*iph 6

如果multi method在角色中声明使用方法,则P6会强制multi method消费者中有一个具有匹配签名的a.(它也允许其他签名.)

如果省略multi的作用,P6并没有强制执行的签名,只有具有匹配名称的方法在消费者声明.

我不知道它为什么会这样.