存根方法的类型签名是否未在 Perl6 的角色中强制执行?

Are typed signatures for stubbed methods not enforced in roles for Perl6?

我正在尝试使用 Perl6 进行一些 OOP,但在角色方面遇到了一些麻烦。我正在尝试以类似于 Java 接口的方式使用它们,在该接口中,我将只拥有必须由任何执行该角色的 class 实现的方法签名。我正在使用带类型参数和 return.

的存根方法

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

示例脚本:

#!/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.

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

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

如果您在角色中省略 multi,P6 不会 强制执行签名,只是在消费者中声明了具有匹配名称的方法。

我不知道为什么会这样。

2020 更新 请参阅我在 "I think the design intent was to support two notions of polymorphic composition" 开头的评论。 (2020题是这个题的骗子,但是我不记得这个了,可惜我搜索的时候也没有找到。)