Type::Tiny rescues Moo

I have just ranted about removing old bad code from the Perl core. Let me lighten the mood by talking about some good, new code.

I have loved Moose for some time now, but like others, I disliked how heavy it was. Then Moo came along and it was great … until I found myself not availing myself of the type system, because it was harder to get to than in Moose. I was skipping validation more and more.

A recent project came up and I really wanted to do it right, so I tried Toby Inkster’s new Type::Tiny and may I say, “hat’s off to you sir!” The combination of Moo and Type::Tiny brings that Moosey feeling back, while still being light and responsive and even fat-pack-able! Great work to all involved in both projects!

8 Comments

Do keep in mind that type validation (and it’s evil brother, coercion) are the main offenders when it comes to Moose-alikes feeling slow. My advice: Don’t use type validation except at the outermost API layer of a project where you interface with separate units of code. Don’t use coercion since coercion promotes type validation to a behavior-modifying feature and prevents you from later turning it off globally in production if performance becomes a concern.

For what it’s worth, Type::Tiny’s built-in type constraints are faster than Moose’s, faster than MooX::Types::MooseLike, and faster than the pure Perl Mouse type constraints (but not as fast as XS). But clearly they’re never going to be as fast as no checking at all.

As it happens, Moo actually performs coercions before isa, so you can disable type checks without breaking coerced attributes.

Even with Moose, there’s nothing to stop you disabling isa for those attributes that use coercion (which will often be a minority), and keeping it enabled for those that don’t.

I fail to see how you can do type coercion without knowing the types (which requires the run-time type checks in a dynamic language).

In Moo, “coercion” and “isa” are just coderefs; the “coercion” coderef is called before the “isa” coderef.

Is there a way to turn type checking off globally, with Moo and Type::Tiny? (my types are in some Type::Library)

Is it possible to turn it off in one class, or group of classes? Maybe for one particular Type::Library?

Toby, you know both modules well so I’m asking you.

Could there be any way to tell in a Moo class: “don’t do any type check for any of the attributes of this class” ?

Or with Type::Tiny, to tell a Type::Library, that its type checks should not be executed.

Maybe in the same way that Debuggit skips useless debug function calls ? (see “Performance Considerations” in DEbuggit::Manual - implemented with B::Deparse)

Wow!

Thank you Toby, I’ll start playing with it.

It should also enable to clone mutable attribute# (like DateTime attributes), both at construction and for their accessor: all you need for proper immutability

Leave a comment

About Joel Berger

user-pic As I delve into the deeper Perl magic I like to share what I can.