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

前端之家收集整理的这篇文章主要介绍了perl6 – 如何将类属性声明为类名联合?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在阅读一个寻找不同结构的电子表格.当我尝试以下使用Moose时,它似乎做了我想要的.我可以创建不同类型的对象,将其分配给找到的成员
并转储Cell实例以供审核.
  1. package Cell
  2. {
  3. use Moose;
  4. use Moose::Util::TypeConstraints;
  5. use namespace::autoclean;
  6.  
  7. has 'str_val' => ( is => 'ro',isa => 'Str',required => 1 );
  8. has 'x_id' => ( is => 'ro',); # later required => 1 );
  9. has 'color' => ( is => 'ro',);
  10. has 'border' => ( is => 'ro',);
  11. has 'found' => ( is => 'rw',isa => 'Sch_Symbol|Chip_Symbol|Net',);
  12. 1;
  13. }
@H_403_5@如果我尝试在Perl 6中执行相同操作,则无法编译.

  1. class Cell {
  2. has Str $.str_val is required;
  3. has Str $.x_id is required;
  4. has Str $.color;
  5. has Str $.border;
  6. has Sch_Symbol|Chip_Symbol|Net $.found is rw
  7. }
@H_403_5@06002

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

解决方法

你可以使用 where
  1. has $.found is rw where Sch_Symbol|Chip_Symbol|Net;
@H_403_5@或者在subset之前定义新类型

  1. subset Stuff where Sch_Symbol|Chip_Symbol|Net;
  2.  
  3. class Cell {
  4. has Str $.str_val is required;
  5. has Str $.x_id is required;
  6. has Str $.color;
  7. has Str $.border;
  8. has Stuff $.found is rw;
  9. }

猜你在找的Perl相关文章