可以动态创建类别名吗?
得到了这个课程:
class Mass-lb is Mass {
method new(Rat:D() $value = 1.0) {
self.bless(
:abbr('lb'),
:base_value(453.59237),
:$value,
);
}
}
我创建了这样的别名:
class Mass-lbs is Mass-lb { }
class Mass-pound is Mass-lb { }
class Mass-pounds is Mass-lb { }
class Mass-pnds is Mass-lb { }
但是我宁愿做这样的事情:
my @lb-syn = < lbs pounds pound pnds >;
for @lb-syn {
EVAL 'class ::("Mass-$_") is Mass-lb {}';
}
这会引发错误:
name::(“ ass-$ $ _”)并非已知,并且可以没有用作包装名称
PHP具有用于创建别名的内置: https://www.php.net/manual/en/en/function.class-alias.php
我找不到Raku类似的东西。
Got this class:
class Mass-lb is Mass {
method new(Rat:D() $value = 1.0) {
self.bless(
:abbr('lb'),
:base_value(453.59237),
:$value,
);
}
}
I have created aliases like this:
class Mass-lbs is Mass-lb { }
class Mass-pound is Mass-lb { }
class Mass-pounds is Mass-lb { }
class Mass-pnds is Mass-lb { }
But I'd prefer to do something like this:
my @lb-syn = < lbs pounds pound pnds >;
for @lb-syn {
EVAL 'class ::("Mass-$_") is Mass-lb {}';
}
This throws an error:
Name ::("Mass-$_") is not compile-time known, and can not serve as a package name
PHP has a built-in for creating aliases: https://www.php.net/manual/en/function.class-alias.php
I couldn't find anything similar for raku.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在Rakuast中,您可以打电话给创建新类型的类。但是Rakuast分支尚未降落。
在此之前,您使用
eval
的方法是有效的,您只需要更简单一点:begin begin begin
time创建别名。常数
进行混叠。常数
s是我们的
默认情况下,因此它们在eval
之外可见。In RakuAST there's a class that you can call to create a new type. But that the RakuAST branch hasn't landed yet.
Until then, your approach using
EVAL
is valid, you just need to make it a bit simpler:BEGIN
time.constant
for aliasing.constant
s areour
by default, they're visible outside of theEVAL
.另外,您可以使用raku Physics :: unit and physics :: Mealist ...
更多信息,请访问github 物理::单元 and Physics :: MEATH ...
Alternatively, you could use raku Physics::Unit and Physics::Measure...
More info at github Physics::Unit and Physics::Measure...