加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 21:54:09 所属栏目:大数据 来源:网络整理
导读:我正在阅读一个寻找不同结构的电子表格.当我尝试以下使用Moose时,它似乎做了我想要的.我可以创建不同类型的对象,将其分配给找到的成员 并转储Cell实例以供审核. package Cell{ use Moose; use Moose::Util::TypeConstraints; use namespace::autoclean; has
我正在阅读一个寻找不同结构的电子表格.当我尝试以下使用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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读