(如何) raku 做类同义词?

p6s*_*eve 5 oop raku

我有

class Length is Measure export { ... }
Run Code Online (Sandbox Code Playgroud)

我想要的同义词仅在于类名不同,我试过这个:

class Distance   is Length is export {}
class Breadth    is Length is export {}
class Width      is Length is export {}
class Height     is Length is export {}
class Depth      is Length is export {}
Run Code Online (Sandbox Code Playgroud)

这种在 $distance ~~ Length 中有效,但我也想要 $length ~~ Distance。

某些类型或强制将是可取的 - 例如 $length.Distance ~~ Distance 阻止像 $width = $height + $depth 这样的操作(即你不能总是添加指向不同轴的长度)。

也许某种类 := 名称绑定,或者强制 NxN 的速记方式?

非常感谢收到任何建议......

rai*_*iph 6

这还不是一个答案,但可能会成为一个答案。

以下标题对您有用吗?我并不一定是要您使用组合来代替继承 ( role)、直接别名 ( constant)、混合 ( but) 或动态类型约束 ( where)。下面的代码只是在您提供反馈之前快速构建原型的方法。

role Measurement {}
role Height does Measurement {}
role Width does Measurement {}
constant Breadth = Width;
say Width;                             # (Width)
say Breadth;                           # (Width)
say ::<Breadth>:kv;                    # (Breadth (Width))
say Breadth ~~ Width;                  # True
say Width ~~ Breadth;                  # True

multi infix:<+>
  (::L Measurement \l, 
       Measurement \r where * !~~ L)
{ fail }

say (42 but Width) + (99 but Breadth); # 141
say (42 but Width) + (99 but Height);  # Failed...
Run Code Online (Sandbox Code Playgroud)