Notice: forthcoming change to Type::Tiny overloading

One of the features of Type::Tiny that differentiates it from Moose's built-in type constraint system is that it allows stand-alone coercions which can then be mixed with type constraints as required. So if you had a Split coercion which split a multi-line string into an arrayref of lines, you could do something like this:

use MyApp::Types qw( ArrayRef Split );
 
has lines => (
   is      => 'ro',
   isa     => ArrayRef + Split,
   coerce  => 1,
);

This is one of several features designed to encourage people to combine coercions with type constraints at the point of use, rather than globally. We wouldn't want people adding coercions to the global definition of ArrayRef, because global stuff is bad, remember?

Anyway, the current stable version of Type::Tiny (0.040) is going to be the final one to overload the + operator for this. From now on, you need to use:

use MyApp::Types qw( ArrayRef Split );
 
has lines => (
   is      => 'ro',
   isa     => ArrayRef->plus_coercions(Split),
   coerce  => 1,
);

The plus_coercions method has existed for quite some time (pre-0.001); it's not new by any means. It's just that the alternative (overloading) is going away. So if you're using that, time to update your code.

Sorry.

Development version 0.041_01 is on CPAN now, and includes this change, so you can test your code with that.

2 Comments

Could explain the reasons for this discussion? The overloaded syntax is quite readable, compared to the plus_coercion method (which looks like code in the definition of the attribute, hence a little more scary)

Leave a comment

About Toby Inkster

user-pic I'm tobyink on CPAN, IRC and PerlMonks.