构造函数 – Perl 6:如何检查`new`是否存在无效参数?

前端之家收集整理的这篇文章主要介绍了构造函数 – Perl 6:如何检查`new`是否存在无效参数?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
检查是否将无效参数传递给构造函数方法new的最简单方法是什么?

use v6;
unit class Abc;

has Int $.a;

my $new = Abc.new( :b(4) );

解决方法

ClassX::StrictConstructor module应该有所帮助.使用zef install ClassX :: StrictConstructor安装它并像这样使用它:

use ClassX::StrictConstructor;

class Stricter does ClassX::StrictConstructor {
    has $.thing;
}

throws-like { Stricter.new(thing => 1,bad => 99) },X::UnknownAttribute;

猜你在找的Perl相关文章