gpt4 book ai didi

perl - Moose - 从 Num 强制转换为 ArrayRef[Num]?

转载 作者:行者123 更新时间:2023-12-02 07:45:12 27 4
gpt4 key购买 nike

好吧,我做错了什么 - Moose 忽略了我的强制:

package moo;

use Moose;
use Moose::Util::TypeConstraints;

subtype Bar => as 'ArrayRef[Num]';

coerce 'Bar' =>
from 'Num' => via { [ 10 ] }; # this doesn't seem to be getting called

has x => (
is => 'rw',
isa => 'Bar',
);

package main;

my $m1 = moo->new(x => [ 3 ]); # works
my $m2 = moo->new(x => 5); # doesn't work

最佳答案

也许您在定义 x 属性时忘记了 coerce => 1

has x => ( is  => 'rw', isa => 'Bar', coerce => 1 );

`

关于perl - Moose - 从 Num 强制转换为 ArrayRef[Num]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7541082/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com