5

我继承了以下针对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 (如果我将其注释掉,我会丢失此错误)并在文档中指出了这一点:

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

我认为这已经发生了变化,并希望对如何更新我现有的代码以迎头赶上有所帮助?

4

1 回答 1

4

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

两者都不。你应该退货。

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

请参阅Type::Tiny::Manual::Libraries 中的 Parameterizable Types — 它提供了一个很好的解释和示例,说明如何使用内联和强制执行参数化类型。

我已经检查了当前 Type::Tiny 版本中的上述作品,甚至是早至 0.006 的版本(2013 年 5 月发布)!您在原始代码中的操作方式从来都不是预期的用途,coercion_generator我很惊讶它曾经奏效过。


另外,如果您对为什么会收到有关深度强制的警告感兴趣,那是因为它的$type->coercion行为很像 Moo/Moose 中的惰性属性,它调用了您的 coderef,但您的 coderef 调用了$type->coercion.

于 2020-11-21T00:51:02.103 回答