可以动态创建类别名吗?

发布于 2025-02-04 09:40:19 字数 835 浏览 1 评论 0原文

得到了这个课程:

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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨曦÷微暖 2025-02-11 09:40:19

在Rakuast中,您可以打电话给创建新类型的类。但是Rakuast分支尚未降落。

在此之前,您使用eval的方法是有效的,您只需要更简单一点:

class Mass-lb { }
BEGIN "constant Mass-$_ = Mass-lb".EVAL
  for <lbs pounds pound pnds>;
my $mlb = Mass-lbs.new;
  1. 确保在begin begin begin time创建别名。
  2. 无需子类,您可以使用常数进行混叠。
  3. 由于常数 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:

class Mass-lb { }
BEGIN "constant Mass-$_ = Mass-lb".EVAL
  for <lbs pounds pound pnds>;
my $mlb = Mass-lbs.new;
  1. Make sure the aliases are created at BEGIN time.
  2. No need to subclass, you can use a constant for aliasing.
  3. Since constants are our by default, they're visible outside of the EVAL.
╭⌒浅淡时光〆 2025-02-11 09:40:19

另外,您可以使用raku Physics :: unit and physics :: Mealist ...

use Physics::Unit;
use Physics::Measure :ALL;

# define a new custom Unit
Unit.new( defn => 'lbm', names => <Mass-lb Mass-lbs Mass-pound Mass-pounds Mass-pnds> );

say GetUnit('Mass-lbs').names;      #[Mass-lb Mass-lbs Mass-pound Mass-pounds Mass-pnds]

# use the Unit in a Measure 
my $mass = ♎️'42 Mass-pnds';
say $mass;                          #42Mass-lb
say $mass.^name;                    #(..Mass) ...class name

# convert to another Unit
my $kgm = $mass.in: 'kg';
say $kgm;                           #19.05087954kg

# convert back 
say $kgm.in: 'Mass-pound';          #42Mass-lb

# raku Rats mean that the back conversion is identical
say $kgm cmp $mass;                 #Same

# with % or abs Error
my $mass2 = ♎️'42 Mass-pnds ±3%';
say $mass2;                         #42Mass-lb ±1.26
say $mass2.in: 'kg';                #19.05087954kg ±0.5715263862

更多信息,请访问github 物理::单元 and Physics :: MEATH ...

Alternatively, you could use raku Physics::Unit and Physics::Measure...

use Physics::Unit;
use Physics::Measure :ALL;

# define a new custom Unit
Unit.new( defn => 'lbm', names => <Mass-lb Mass-lbs Mass-pound Mass-pounds Mass-pnds> );

say GetUnit('Mass-lbs').names;      #[Mass-lb Mass-lbs Mass-pound Mass-pounds Mass-pnds]

# use the Unit in a Measure 
my $mass = ♎️'42 Mass-pnds';
say $mass;                          #42Mass-lb
say $mass.^name;                    #(..Mass) ...class name

# convert to another Unit
my $kgm = $mass.in: 'kg';
say $kgm;                           #19.05087954kg

# convert back 
say $kgm.in: 'Mass-pound';          #42Mass-lb

# raku Rats mean that the back conversion is identical
say $kgm cmp $mass;                 #Same

# with % or abs Error
my $mass2 = ♎️'42 Mass-pnds ±3%';
say $mass2;                         #42Mass-lb ±1.26
say $mass2.in: 'kg';                #19.05087954kg ±0.5715263862

More info at github Physics::Unit and Physics::Measure...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文