Subject: | unexpected error from on-the-fly type union coercions, e.g. ( Str | Str )->coercion |
I tried to create a coercion routine from an "on-the-fly" union type:
------------------------------
$sub = ( Str | Str )->coercion;
$sub->( 'x' );
------------------------------
And got this explosion:
------------------------------
Undef did not pass type constraint "TypeTiny" at foo line 4
Undef did not pass type constraint "TypeTiny"
"TypeTiny" is defined as: (Scalar::Util::blessed($_) && $_->isa(q[Type::Tiny]))
------------------------------
if I Dump $sub, I get
------------------------------
bless( {
'name' => '__ANON__',
'type_constraint' => undef
}, 'Type::Coercion::Union' );
------------------------------
Which looks suspiciously empty.
If I make sure the union type hangs around:
------------------------------
$t = Str | Str;
$sub = $t->coercion;
$sub->( 'x' );
------------------------------
everything works.
I'm guessing there's a (too) weakened reference that's getting zapped when the temporary Type::Union is getting destroyed.
Feature? Bug? I couldn't find any documentation either way. (Of course, there's a good probability I just missed it. Happens a lot to me.)
For what it's worth, this code arose because I was adding a coercion to a Moo attribute which needed a one-off union of types and I didn't feel it warranted the extra code to create a type that hung around.
Thanks!
Diab