DEV Community

Discussion on: Types, Objects, and Systems, Oh my!

Collapse
 
tobyink profile image
Toby Inkster

Type::Tiny, Specio, MooseX::Types, and MouseX::Types do actually all support a common core set of methods.

For example, if $type is a type constraint object of any of the above, the following will work for all four of them:

# Type checks
die( $type->get_message( $value ) )
  unless $type->check( $value );

# Type coercions
if ( $type->has_coercion ) {
   $value = $type->coerce( $orig );
}

# Inlining
if ( $type->can( 'can_be_inlined' ) and $type->can_be_inlined ) {
  my $method  = $type->can( 'inline_check' ) || $type->can( '_inline_check' );
  my $varname = '$foo';
  say $type->$method( $varname );
}
Enter fullscreen mode Exit fullscreen mode

It's not just a co-incidence that they all support this API: they're all aiming for (almost) drop-in compatibility with Moose::Meta::TypeConstraint.