为什么Perl 6会为我的子集类型抛出X :: AdHoc异常?
这是Perl 6:
X::AdHoc instead of X::TypeCheck::Binding with subset parameter中报告的错误,首次报告于2015年11月.
在玩我的Perl 6模块Chemisty::Elements时,我遇到了一个我没想到的Exception问题. 我定义了一个类型ZInt,它将数字限制在周期图表上找到的序数(我在这里伪造了一点).然后我使用该类型将参数约束到子例程.我期望得到某种X::TypeCheck,但我得到了X::AdHoc: use v6; subset ZInt of Cool is export where { state ( $min,$max ) = <1 120>; ( $_.truncate == $_ and $min <= $_ <= $max ) or warn "Z must be between a positive whole number from $min to $max. Got <$_>." }; sub foo ( ZInt $Z ) { say $Z } try { CATCH { default { .^name.say } } foo( 156 ); } 首先,我得到两次警告,这很奇怪:
但是,当我宁愿人们知道这是一个类型错误时,我得到X :: AdHoc类型. 我检查了没有警告会发生什么,并再次得到X :: AdHoc: subset ZInt of Cool is export where { state ( $min,$max ) = <1 120>; ( $_.truncate == $_ and $min <= $_ <= $max ) }; 所以,我想我可以抛出自己的异常: subset ZInt of Cool is export where { state ( $min,$max ) = <1 120>; ( $_.truncate == $_ and $min <= $_ <= $max ) or X::TypeCheck.new.throw; }; 但是,我得到一个警告:
在这一点上,我不知道有什么抱怨.我认为其中一种方法需要我不提供的东西,但我没有看到任何关于new的参数或扔进文档. 如何在没有警告的情况下获得我想要的类型以及我的自定义文本? 解决方法
不要抛出异常或与之发出警告.相反,你想失败:
subset ZInt of Cool is export where { state ( $min,$max ) = <1 120>; ( $_.truncate == $_ and $min <= $_ <= $max ) or fail "Z must be between a positive whole number from $min to $max. Got <$_>." }; 我相信这是你的意图.没有你自己的异常也没关系,但是X :: TypeCheck中有一个bug.它应该要么“操作”要么提供合理的默认,就像它“得到”和“预期”一样. subset ZInt of Cool is export where { state ( $min,$max ) = <1 120>; ( $_.truncate == $_ and $min <= $_ <= $max ) or fail X::TypeCheck.new( operation => "type check",expected => ::('ZInt'),got => $_,); }; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |