小编J H*_*all的帖子

Perl 6 List Slatenation without Slip?

在Perl中,,运算符可用于连接列表; 但是,Perl 6并未在此上下文中压缩列表,从而导致列表中包含两个列表.连接列表需要使用|滑动操作符.

my @a = <a b c>;
my @b = <d e f>;
my @ab = |@a, |@b;
Run Code Online (Sandbox Code Playgroud)

这个操作有什么简写吗?

concatenation perl6 raku

9
推荐指数
1
解决办法
462
查看次数

如何从块的CATCH移相器返回值?

从不是例程的块返回CATCH移相器中的值的语法是什么?

sub foo() {
    <1 2 3>.map: -> $a {
        die 'oops';
        CATCH { default { 'foo' } }
    }
}

sub bar() {
    <1 2 3>.map: -> $a {
        die 'oops';
        CATCH { default { return 'bar' } }
    }
}

say foo(); # (Nil, Nil, Nil)
say bar(); # Attempt to return outside of immediatelly-enclosing Routine (i.e. `return` execution is outside the dynamic scope of the Routine where `return` was used)
Run Code Online (Sandbox Code Playgroud)

编辑:所需的输出是:

say baz(); # (baz baz baz) …
Run Code Online (Sandbox Code Playgroud)

rakudo perl6 raku

9
推荐指数
1
解决办法
190
查看次数

简洁的方法将抛出异常改为失败?

是否有更简洁的方法将抛出的异常提升为失败而不是以下?

try {
    die 'always';
    CATCH { default { fail $_ } }
}
Run Code Online (Sandbox Code Playgroud)

error-handling exception-handling try-catch rakudo perl6

8
推荐指数
3
解决办法
149
查看次数

覆盖角色的属性

是否可以覆盖角色的属性以提供默认值?

role A {
     has $.a;
}
class B does A {
    has $.a = "default";
}
my $b = B.new;
Run Code Online (Sandbox Code Playgroud)

这会导致编译错误:

===SORRY!=== Error while compiling:
Attribute '$!a' already exists in the class 'B', but a role also wishes to compose it
Run Code Online (Sandbox Code Playgroud)

perl6

6
推荐指数
1
解决办法
93
查看次数

如何从注释处理器中的AnnotationValue捕获枚举

我试图使用注释处理器和注释镜像读取注释中枚举的值,但我得到了返回null.我认为这与将Enum包装为VariableElement的AnnotationValue有关.VariableElement的文档#getConstantValue()表示"如果这是一个初始化为编译时常量的最终字段,则返回此变量的值." 好的,但是final不是注释成员的有效修饰符.另外值得注意的是,我可以轻松阅读其他注释值,只需阅读Enums.

我做了一些调查,看起来AnnotationValue在运行时被实例化为Symbol.VarSymbol,但是Symbol.VarSymbol #getConstantValue()看起来应该只返回对象.

最后,如果我在AnnotationValue上执行toString(),我会得到正确的值.

注释:

package annotation;
public @interface AnAnnotation
{
    String value();
    Behavior defaultBehavior() default Behavior.NEW;

    public static enum Behavior
    {
        NEW, NULL;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的处理器的一部分并嵌套在过多的循环中以获得正确的AnnotaionMirror:

Map<? extends ExecutableElement, ? extends AnnotationValue> annotationValues = elemUtils.getElementValuesWithDefaults(annotationMirror);
for (ExecutableElement method : annotationValues.keySet())
{
    ...
    else if ("defaultBehavior".equals(method.getSimpleName().toString()))
    {

        defaultBehavior = (Behavior)( (VariableElement)annotationValues.get(method).getValue()).getConstantValue();

        // This prints "NEW" or "NULL" correctly
        processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE,annotationValues.get(method).toString());
        // This prints null incorrectly (expect "NEW" or "NULL")
        processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, defaultBehavior + "");

    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

编辑:处理器的更完整版本.

package annotation.processor;

import …
Run Code Online (Sandbox Code Playgroud)

java enums annotations mirror annotation-processing

5
推荐指数
1
解决办法
2229
查看次数

如何将项目上下文数组分配给位置?

在Rakudo Perl 6中item$可用于评估项目上下文中的表达式.请参阅https://docs.perl6.org/routine/item

我正在使用一个返回项目contextualized Array的库.删除语境化的正确方法是什么,以便将其分配给@变量?

例如:

my @a = $[<a b c>];
dd @a; # Outputs: Array @a = [["a", "b", "c"],]
Run Code Online (Sandbox Code Playgroud)

rakudo perl6

2
推荐指数
1
解决办法
93
查看次数