无法使用 Raku 官方文档中有关角色的示例

发布于 2025-01-20 04:04:39 字数 1423 浏览 3 评论 0原文

docs.raku ,我在rakudo 2022.03

platform-pro (pro) in ~ via ⬢ v12.4.0
❯ raku                                                                                                           ~
Welcome to Rakudo™ v2022.03.
Implementing the Raku® Programming Language v6.d.
Built on MoarVM version 2022.03.

To exit type 'exit' or '^D'
>  role Canine {
*  method bark { ... }
*  }
(Canine)
>  class Dog does Canine {
*  method bark { say  "Woof" }
*  }
===SORRY!=== Error while compiling:
Package 'Dog' already has a method 'bark' (did you mean to declare a multi method?)
at line 2

我更改 以下错误时,多数的方法,但是:

>  role Canine {
*  method bark { ... }
*  }
(Canine)
>  class Dog does Canine {
*  multi method bark { say  "Woof" }
*  }
===SORRY!=== Error while compiling:
No appropriate parametric role variant available for 'Canine':
    Ambiguous call to '(Dog)'; these signatures all match:
      (::$?CLASS ::::?CLASS Mu $)
      (::$?CLASS ::::?CLASS Mu $)

多亏了评论,这似乎是一个问题:

role Canine {
    method bark { say  "Woof" };
}

class Dog does Canine {
    method bark { say  "Woof" };
}

my $d = Dog.new();

$d.bark

如果将其保存到文件并执行

☸ platform-pro (pro) in learn/raku/stackoverflow via 
              

Following this defenitions from docs.raku, I get the following error with rakudo 2022.03

platform-pro (pro) in ~ via ⬢ v12.4.0
❯ raku                                                                                                           ~
Welcome to Rakudo™ v2022.03.
Implementing the Raku® Programming Language v6.d.
Built on MoarVM version 2022.03.

To exit type 'exit' or '^D'
>  role Canine {
*  method bark { ... }
*  }
(Canine)
>  class Dog does Canine {
*  method bark { say  "Woof" }
*  }
===SORRY!=== Error while compiling:
Package 'Dog' already has a method 'bark' (did you mean to declare a multi method?)
at line 2

I changed the method to multi but:

>  role Canine {
*  method bark { ... }
*  }
(Canine)
>  class Dog does Canine {
*  multi method bark { say  "Woof" }
*  }
===SORRY!=== Error while compiling:
No appropriate parametric role variant available for 'Canine':
    Ambiguous call to '(Dog)'; these signatures all match:
      (::$?CLASS ::::?CLASS Mu $)
      (::$?CLASS ::::?CLASS Mu $)

Thanks to the comments it seems a problem in the REPL:

role Canine {
    method bark { say  "Woof" };
}

class Dog does Canine {
    method bark { say  "Woof" };
}

my $d = Dog.new();

$d.bark

if save this to a file and execute

☸ platform-pro (pro) in learn/raku/stackoverflow via ???? v3.9.10 (python-3.9.10) via ???? v6.d v2022.03
❯ raku prueba-roles.raku                                                                ~/learn/raku/stackoverflow
Woof

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文