首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何避免Type::Tiny::_build_coercion中的“深递归”错误/警告

如何避免Type::Tiny::_build_coercion中的“深递归”错误/警告
EN

Stack Overflow用户
提问于 2020-11-20 19:04:57
回答 1查看 111关注 0票数 5

我继承了以下代码,该代码是针对Type-Tiny-1.004004编写的

代码语言:javascript
复制
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。我写了下面的测试来说明这个问题:

代码语言:javascript
复制
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();

这将产生以下结果:

代码语言:javascript
复制
# 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 (如果我注释掉它,就会丢失这个错误),并在文档中注意到了这一点。

以下属性用于参数化矫顽器,但没有完整记录,因为它们可能在不久的将来发生变化:

我猜想这种情况已经改变了,并希望在如何更新我现有的代码以迎头赶上方面提供一些帮助?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-21 00:51:02

不确定是否应该将强制添加到$base或$derived中

都不是。你应该把它还给我。

代码语言:javascript
复制
coercion_generator => sub {
    my ($base, $derived, $max) = @_;
    return Type::Coercion->new(
        type_coercion_map => [ Str, sub { substr $_, 0, $max } ]
    );
};

参见类型中可参数化的类型::Tiny::Manual::库 --它给出了一个非常好的解释和示例,说明了如何使用内联和强制进行参数化类型。

我已经检查了上面的作品在当前的类型::微小的版本,甚至0.006版本(2013年5月发布)!您在原始代码中是如何做的,这从来都不是coercion_generator的预期用途,我感到惊讶的是,它竟然成功了。

另外,如果您感兴趣的是为什么会收到关于深度强制的警告,这是因为$type->coercion在Moo/Moose中的行为非常像一个懒惰的属性,它调用您的coderef,但是您的代码名为$type->coercion

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64935422

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档