英文:
How to add an attribute and its accessors in Raku using add_attribute()?
问题
我有这个程序:
class Foo {
has $.first;
}
my $a = Attribute.new(
:name('$!second'),
:type(Int),
:package('Foo'),
:has_accessor(True)
);
Foo.^add_attribute($a);
Foo.^compose;
Foo.^attributes; # Mu $!firstInt $!second
my $foo = Foo.new: :42first, :21second;
$foo.second; # P6opaque: no such attribute '$!second' on type Foo in a Foo when trying to get a value
attributes
方法显示已添加属性,似乎成功创建了一个对象,但当我尝试访问 $!second
属性时,我收到一个错误。
我漏掉了什么?
英文:
I have this program:
class Foo {
has $.first;
}
my $a = Attribute.new(
:name('$!second'),
:type(Int),
:package('Foo'),
:has_accessor(True)
);
Foo.^add_attribute($a);
Foo.^compose;
Foo.^attributes».say; # Mu $!firstInt $!second
my $foo = Foo.new: :42first, :21second;
$foo.second.say; # P6opaque: no such attribute '$!second' on type Foo in a Foo when trying to get a value
The attributes
method shows that the attribute has been added, creating an object apparently succeeds, yet when I try to access the $!second
attribute I receive an error.
What am I missing?
答案1
得分: 7
我漏掉了什么吗?
看起来你没有漏掉任何东西。在我看来这是一个 bug。
在 RakuAST 开发的这个阶段,我认为它不太可能被修复。
如果你真的对以编程方式构建类感兴趣,我建议你开始研究如何使用 RakuAST。
文档仍然大部分缺失,但有一个强大的 .AST
方法,你可以在示例代码上运行它,它会生成创建该代码所需的 RakuAST
语句。例如:
$ raku -e 'say Q|class A { has $.a; has $.b }|.AST'
RakuAST::StatementList.new(
RakuAST::Statement::Expression.new(
expression => RakuAST::Package.new(
declarator => "class",
name => RakuAST::Name.from-identifier("A"),
body => RakuAST::Block.new(
body => RakuAST::Blockoid.new(
RakuAST::StatementList.new(
RakuAST::Statement::Expression.new(
expression => RakuAST::VarDeclaration::Simple.new(
scope => "has",
sigil => "$",
twigil => ".",
desigilname => RakuAST::Name.from-identifier("a")
)
),
RakuAST::Statement::Expression.new(
expression => RakuAST::VarDeclaration::Simple.new(
scope => "has",
sigil => "$",
twigil => ".",
desigilname => RakuAST::Name.from-identifier("b")
)
)
)
)
)
)
)
)
英文:
> What am I missing?
Looks like you're not missing anything. Looks like a bug to me.
At this stage of RakuAST development, I don't think it will get fixed.
If you're really into building classes programmatically, I'd suggest you start looking into using RakuAST.
Documentation is still mostly absent, but there's a powerful .AST
method that you can run on sample code, that will produce the RakuAST
statements needed to create that code. For example:
$ raku -e 'say Q|class A { has $.a; has $.b }|.AST'
RakuAST::StatementList.new(
RakuAST::Statement::Expression.new(
expression => RakuAST::Package.new(
declarator => "class",
name => RakuAST::Name.from-identifier("A"),
body => RakuAST::Block.new(
body => RakuAST::Blockoid.new(
RakuAST::StatementList.new(
RakuAST::Statement::Expression.new(
expression => RakuAST::VarDeclaration::Simple.new(
scope => "has",
sigil => "$",
twigil => ".",
desigilname => RakuAST::Name.from-identifier("a")
)
),
RakuAST::Statement::Expression.new(
expression => RakuAST::VarDeclaration::Simple.new(
scope => "has",
sigil => "$",
twigil => ".",
desigilname => RakuAST::Name.from-identifier("b")
)
)
)
)
)
)
)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论