如何在Raku中使用add_attribute()添加属性及其访问器?

huangapple go评论106阅读模式
英文:

How to add an attribute and its accessors in Raku using add_attribute()?

问题

我有这个程序:

  1. class Foo {
  2. has $.first;
  3. }
  4. my $a = Attribute.new(
  5. :name('$!second'),
  6. :type(Int),
  7. :package('Foo'),
  8. :has_accessor(True)
  9. );
  10. Foo.^add_attribute($a);
  11. Foo.^compose;
  12. Foo.^attributes␤; # Mu $!first␤Int $!second␤
  13. my $foo = Foo.new: :42first, :21second;
  14. $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:

  1. class Foo {
  2. has $.first;
  3. }
  4. my $a = Attribute.new(
  5. :name('$!second'),
  6. :type(Int),
  7. :package('Foo'),
  8. :has_accessor(True)
  9. );
  10. Foo.^add_attribute($a);
  11. Foo.^compose;
  12. Foo.^attributes».say; # Mu $!first␤Int $!second␤
  13. my $foo = Foo.new: :42first, :21second;
  14. $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 语句。例如:

  1. $ raku -e 'say Q|class A { has $.a; has $.b }|.AST'
  2. RakuAST::StatementList.new(
  3. RakuAST::Statement::Expression.new(
  4. expression => RakuAST::Package.new(
  5. declarator => "class",
  6. name => RakuAST::Name.from-identifier("A"),
  7. body => RakuAST::Block.new(
  8. body => RakuAST::Blockoid.new(
  9. RakuAST::StatementList.new(
  10. RakuAST::Statement::Expression.new(
  11. expression => RakuAST::VarDeclaration::Simple.new(
  12. scope => "has",
  13. sigil => "$",
  14. twigil => ".",
  15. desigilname => RakuAST::Name.from-identifier("a")
  16. )
  17. ),
  18. RakuAST::Statement::Expression.new(
  19. expression => RakuAST::VarDeclaration::Simple.new(
  20. scope => "has",
  21. sigil => "$",
  22. twigil => ".",
  23. desigilname => RakuAST::Name.from-identifier("b")
  24. )
  25. )
  26. )
  27. )
  28. )
  29. )
  30. )
  31. )
英文:

> 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:

  1. $ raku -e 'say Q|class A { has $.a; has $.b }|.AST'
  2. RakuAST::StatementList.new(
  3. RakuAST::Statement::Expression.new(
  4. expression => RakuAST::Package.new(
  5. declarator => "class",
  6. name => RakuAST::Name.from-identifier("A"),
  7. body => RakuAST::Block.new(
  8. body => RakuAST::Blockoid.new(
  9. RakuAST::StatementList.new(
  10. RakuAST::Statement::Expression.new(
  11. expression => RakuAST::VarDeclaration::Simple.new(
  12. scope => "has",
  13. sigil => "$",
  14. twigil => ".",
  15. desigilname => RakuAST::Name.from-identifier("a")
  16. )
  17. ),
  18. RakuAST::Statement::Expression.new(
  19. expression => RakuAST::VarDeclaration::Simple.new(
  20. scope => "has",
  21. sigil => "$",
  22. twigil => ".",
  23. desigilname => RakuAST::Name.from-identifier("b")
  24. )
  25. )
  26. )
  27. )
  28. )
  29. )
  30. )
  31. )

huangapple
  • 本文由 发表于 2023年8月10日 17:19:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874322.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定