perl6 – 如何将类属性声明为类名联合?

前端之家收集整理的这篇文章主要介绍了perl6 – 如何将类属性声明为类名联合?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在阅读一个寻找不同结构的电子表格.当我尝试以下使用Moose时,它似乎做了我想要的.我可以创建不同类型的对象,将其分配给找到的成员
并转储Cell实例以供审核.
package Cell
{
  use Moose;
  use Moose::Util::TypeConstraints;
  use namespace::autoclean;

  has 'str_val'    => ( is => 'ro',isa => 'Str',required => 1 );
  has 'x_id'       => ( is => 'ro',); # later required => 1 );
  has 'color'      => ( is => 'ro',);
  has 'border'     => ( is => 'ro',);
  has 'found'      => ( is => 'rw',isa => 'Sch_Symbol|Chip_Symbol|Net',);
  1;
}

如果我尝试在Perl 6中执行相同操作,则无法编译.

class Cell {
  has Str $.str_val              is required;
  has Str $.x_id                 is required;
  has Str $.color;
  has Str $.border;
  has Sch_Symbol|Chip_Symbol|Net $.found is rw
}

06002

我怎样才能在Perl 6中做到这一点?

解决方法

你可以使用 where
has $.found is rw where Sch_Symbol|Chip_Symbol|Net;

或者在subset之前定义新类型

subset Stuff where Sch_Symbol|Chip_Symbol|Net;

class Cell {
    has Str   $.str_val is required;
    has Str   $.x_id    is required;
    has Str   $.color;
    has Str   $.border;
    has Stuff $.found   is rw;
}
原文链接:https://www.f2er.com/Perl/171990.html

猜你在找的Perl相关文章