gpt4 book ai didi

oop - (如何) raku 做类同义词?

转载 作者:行者123 更新时间:2023-12-03 15:41:14 25 4
gpt4 key购买 nike

我有

class Length is Measure export { ... }
我想要的同义词只是类名不同,我试过这个:
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 {}
这种工作在那个 $distance ~~ 长度,但我也想要 $length ~~ 距离。
某种形式或强制是可取的 - 例如 $length.Distance ~~ 阻止诸如 $width = $height + $depth 之类的操作的距离(即,您不能总是添加指向不同轴的长度)。
也许某种类的 := 名称绑定(bind),或者强制 NxN 的速记方式?
任何建议都非常感激地收到...

最佳答案

这还不是一个答案,但可能会成长为一个答案。
以下标题对您有用吗?我不一定要让您使用组合而不是继承( role ),或直接别名( constant ),或混合( but ),或动态类型约束( where )。在您提供反馈之前,下面的代码只是一种快速制作原型(prototype)的方法。

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...

关于oop - (如何) raku 做类同义词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66188256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com