在类型系统中§
请参阅主要文档 在上下文中 了解必需的特征
multi trait_mod:<is>(Attribute , :!)multi trait_mod:<is>(Parameter , :!)
将类或角色属性标记为必需的。如果在对象构造时未初始化属性,则会引发 X::Attribute::Required
。
say Correct.new(attr => 42);# OUTPUT: «Correct.new(attr => 42)»C.new;CATCH# OUTPUT: «X::Attribute::Required => The attribute '$!attr' is required, but you did not provide a value for it.»
请注意,具有私有属性的类会给出相同的错误
D.new;CATCH# OUTPUT: «X::Attribute::Required => The attribute '$!attr' is required, but you did not provide a value for it.»
您可以提供一个原因来说明为什么它作为 is required
的参数是必需的
;say Correct.new();# OUTPUT: «The attribute '$!attr' is required because it's so cool,but you did not provide a value for it.»