作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
qw; -6ren">
我继承了以下针对 Type-Tiny-1.004004 编写的代码:
package Company::Types;
use Type::Library -base, -declare => qw< TruncatedString >;
use Type::Utils -all;
declare TruncatedString, as Str,
where { length $_ },
inline_as {
my ($constraint, $varname) = @_;
return sprintf ( '!defined %s or %s',
$varname,
$constraint->parent->inline_check($varname),
);
},
constraint_generator => sub {
my ($max) = @_;
die q{Max length must be positive!} unless int($max) > 0;
return sub {
(length $_) <= $max;
};
},
inline_generator => sub {
my ($max) = @_;
return sub {
my ($constraint, $varname) = @_;
return sprintf(
'%s and length %s <= %d',
$constraint->parent->inline_check($varname),
$varname,
$max,
);
};
},
coercion_generator => sub {
my ($base, $derived, $max) = @_;
# $base - TruncatedString
# $derived - TruncatedString[<N>]
# $max - N
# Not sure if I should be adding the coercion to $base or $derived, but I suspect that
# $derived is the correct choice here.
$derived->coercion->add_type_coercions(Str,
sub {
return substr $_, 0, $max;
}
);
};
但是它不适用于
Type-Tiny-1.012000 。我编写了以下测试来说明问题:
use Test2::V0;
use Time::Out qw< timeout >;
use Company::Types qw< TruncatedString >;
subtest 'Coercing to TruncatedString->of(10)' => sub {
timeout 2 => sub { # test hangs without timeout
my $type = TruncatedString->of(10); # same as TruncatedString[10]
ok( try_ok { $type->coerce('123456789012') }, 'should not throw an exception' );
} || bail_out( 'Ran out of time: ' . $@ );
};
done_testing();
这将输出以下内容:
# Seeded srand with seed '20201120' from local date.
Deep recursion on subroutine "Type::Tiny::_build_coercion" at ${SRC}/company-types/.direnv/perl5/lib/perl5/Type/Tiny.pm line 399.
Deep recursion on anonymous subroutine at ${SRC}/company-types/.direnv/perl5/lib/perl5/Type/Tiny.pm line 467.
Deep recursion on anonymous subroutine at ${SRC}/company-types/.direnv/perl5/lib/perl5/Type/Tiny.pm line 1015.
not ok 1 - Coercing to TruncatedString->of(10) {
not ok 1
# Failed test at t/truncated-string.t line 8.
# Exception: CODE(0x7fee0fb78998)
not ok 2 - should not throw an exception
# Failed test 'should not throw an exception'
# at t/truncated-string.t line 8.
1..2
}
# Failed test 'Coercing to TruncatedString->of(10)'
# at t/truncated-string.t line 10.
1..1
Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/1 subtests
Test Summary Report
-------------------
t/truncated-string.t (Wstat: 256 Tests: 1 Failed: 1)
Failed test: 1
Non-zero exit status: 1
Files=1, Tests=1, 4 wallclock secs ( 0.02 usr 0.01 sys + 3.43 cusr 0.38 csys = 3.84 CPU)
Result: FAIL
我已将问题缩小到
coercion_generator
(如果我将其注释掉,我将丢失此错误)并在
docs 中注意到这一点:
The following attributes are used for parameterized coercions, but are not fully documented because they may change in the near future:
最佳答案
Not sure if I should be adding the coercion to $base or $derived
coercion_generator => sub {
my ($base, $derived, $max) = @_;
return Type::Coercion->new(
type_coercion_map => [ Str, sub { substr $_, 0, $max } ]
);
};
见
Parameterizable Types in Type::Tiny::Manual::Libraries — 它提供了一个很好的解释和示例,说明如何使用内联和强制来处理参数化类型。
coercion_generator
的预期用途。我有点惊讶它曾经奏效过。
$type->coercion
引起的。行为很像 Moo/Moose 中的惰性属性,它调用您的 coderef,但您的 coderef 调用
$type->coercion
.
关于perl - 如何避免 Type::Tiny::_build_coercion 中的 "deep recursion"错误/警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64935422/
我继承了以下针对 Type-Tiny-1.004004 编写的代码: package Company::Types; use Type::Library -base, -declare => qw;
我是一名优秀的程序员,十分优秀!