I recently uploaded a new distro to CPAN recently called Type. The concepts are largely on Moose's built-in type system, but it's a standalone distribution.
Right now this is all very alpha, and the current release is not intended for use by anyone. I've released so people can take a look at critique the design. I've tried to remedy some of the problems that Moose's type system has. MooseX::Types fixes some of these problems but then introduces its own. Type addresses the problems of both.
My long-term goal is to replace Moose's built-in system with Type. This will probably mean rewriting Type to not use Moose itself. The current release uses Moose because it made it easy to prototype the system.
Here's the comparison with Moose and MooseX::Types from the Type distro's docs:
Type names are strings, but they're not global
Unlike Moose and MooseX::Types, type names are always local to the current package. There is no possibility of name collision between different modules, so you can safely use short types names for code.
Unlike MooseX::Types, types are strings, so there is no possibility of colliding with existing class or subroutine names.
No type auto-creation
Types are always retrieved using the t() subroutine. If you pass an unknown name to this subroutine it dies. This is different from Moose and MooseX::Types, which assume that unknown names are class names.
Exceptions are objects
The $type->validate_or_die() method throws a Type::Exception object on failure, not a string.
Anon types are explicit
With Moose and MooseX::Types, you use the same subroutine, subtype(), to declare both named and anonymous types. With Type, you use declare() for named types and anon() for anonymous types.
Class and object types are separate
Moose and MooseX::Types have class_type and duck_type. The former type requires an object, while the latter accepts a class name or object.
In Type, the distinction between accepting an object versus object or class is explicit. There are four declaration helpers, object_can_type, object_isa_type, any_can_type, and any_isa_type.
Overloading support is baked in
Perl's overloading is broken as hell, but ignoring it makes Moose's type system frustrating.
Types can either have a constraint or inline generator, not both
Moose and MooseX::Types types can be defined with a subroutine reference as the constraint, an inline generator subroutine, or both. This is purely for backwards compatibility, and it makes the internals more complicated than they need to be.
With Type, a constraint can have either a subroutine reference or an inline generator, not both.
Coercions can be inlined
I simply never got around to implementing this in Moose.
No crazy coercion features
Moose has some bizarre (and mostly) undocumented features relating to coercions and parameterizable types. This is a misfeature.
Your feedback is requested
The current distro has mostly complete docs, so it should give you a sense of what I'm aiming at.
I'd love to hear from the Perl community on this distribution. Do this seem like it'd help fix problems you've had with Moose types? Can you imagine using this distribution without using Moose? What's on your wishlist?

Recent Comments