【问题标题】:constructing enums using Z=> with variables instead of list literals使用 Z=> 使用变量而不是列表文字构造枚举
【发布时间】:2022-11-02 07:53:41
【问题描述】:

我想构造一个字符串类型的枚举。以下作品:

my Str enum E ( <a b c> Z=> 'one', 'two', 'three' );
E.kv.raku.say;

(“c”、“三”、“a”、“一”、“b”、“二”).Seq

但是,尝试以下操作不会:

my Str @a = <a b c>;
my Str @b = <one two three>;
my Str enum F ( @a Z=> @b );
F.kv.raku.say;

没有为枚举提供任何值(@a Z=> @b 是否需要声明为常量?)

这不支持吗?

Raku/roast 涵盖了 E 中的枚举构造,但我没有看到 F 的任何测试用例。

出于好奇,我也尝试过:

my $a = <a b c>;
my $b = <one two three>;
my Str enum G ( $a<> Z=> $b<>.map( { .Str } ) );
G.kv.raku.say;

("", "").Seq

【问题讨论】:

    标签: enums raku


    【解决方案1】:

    警告要求:

    @a Z=> @b 是否需要声明为常量

    这是正确的;因为enum 是一个编译时声明,所以与计算其键和值有关的所有内容都必须在编译时可用。将@a@b 声明为constants 可以解决问题。因此:

    my constant @a = <a b c>;
    my constant @b = <one two three>;
    my Str enum F ( @a Z=> @b );
    F.kv.raku.say;
    

    生产:

    ("a", "one", "c", "three", "b", "two").Seq
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-22
      • 1970-01-01
      • 2020-07-25
      • 2012-11-05
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 2012-12-12
      相关资源
      最近更新 更多