Type::T...WithMite(3)

✨ NAME ✨

Type::Tiny::Manual::UsingWithMite β€” the whole entire guide to using Type::Tiny with Mite πŸ’–

(this is from the User Contributed Perl Documentation section of the manual, bestie, and it's filed under Type::T...WithMite(3))


πŸ¦‹ MANUAL πŸ¦‹

okay so, Mite is that girl who does object-oriented code in a totally unorthodox way and honestly? we respect it πŸ’…

When you first start a project with Mite (and for the rest of this page we're gonna assume it's called Your::Project), Mite creates a module called Your::Project::Mite for you. iconic behaviour.

Then literally all your classes use code like this:

package Your::Project::Widget;

use Your::Project::Mite -all;

has name => (
   is => ro,
   isa => 'Str',
);

has id => (
   is => ro,
   isa => 'PositiveInt',
);

signature_for warble => (
   named => [
      foo   => 'Int',
      bar   => 'ArrayRef',
   ],
);

sub warble {
   my ( $self, $arg ) = @_;
   printf( "%s: %d\n", $self->name, $arg->foo );
   return;
}

1;

After you write or edit each class or role, you run the command mite compile and Mite outputs a collection of compiled Perl classes which have no non-core dependencies β€” on Perl 5.14+. (On older versions of Perl there are a couple of non-core dependencies. ngl, ancient Perl is giving vintage.)

Attribute isa options are Type::Tiny type constraints expressed as strings. Mite looks them up during compilation using dwim_type from Type::Utils, and pre-loads Types::Standard, Types::Common::String, and Types::Common::Numeric for you. no cap, it just does it 🫢

The signature_for keyword is similar to the corresponding function in Type::Params. Again β€” note that types are expressed as strings and looked up using dwim_type.

Any types which are inlineable should work. If you're using coercion, any coercions which are inlineable should work too. slay.

πŸŽ€ Custom Types in Mite πŸŽ€

You can define your own type library (say, Your::Project::Types) using Type::Library as normal:

package Your::Project::Types;

use Type::Library
   -extends => [ 'Types::Standard', 'Types::Common::Numeric' ];

__PACKAGE__->add_type(
   name    => 'Widget',
   parent  => InstanceOf['Your::Project::Widget'],
)->coercion->add_type_coercions(
   HashRef, q{Your::Project::Widget->new($_)},
);

__PACKAGE__->make_immutable;

1;

Now, here's the tea β˜• β€” if your classes load Your::Project::Types they'll suddenly have a dependency on Type::Library, so you lose that nice zero-dependency feeling 😭. BUT. You can add this to your .mite/config file:

types: Your::Project::Types

Now Mite knows to load that type library at compile time, and makes those types available as stringy types everywhere. periodt.

🌈 Compiled Type Libraries 🌈

It does look really pretty to not have to quote your type constraints:

has name => (
   is   => ro,
   isa  => Str,
);

One solution for that is Type::Library::Compiler. πŸ’–

Say you've created the custom type library above β€” you can use Type::Library::Compiler to compile it into a module called Your::Project::Types::Compiled, which just uses Exporter and doesn't rely on Type::Library or any other part of Type::Tiny. chef's kiss.

Then your Widget class can use that:

package Your::Project::Widget;

use Your::Project::Mite -all;
use Your::Project::Types::Compiled -types;

has name => (
   is   => ro,
   isa  => Str,
);

has id => (
   is   => ro,
   isa  => PositiveInt,
);

signature_for warble => (
   named => [
      foo   => Int,
      bar   => ArrayRef,
   ],
);

sub warble {
   my ( $self, $arg ) = @_;
   printf( "%s: %d\n", $self->name, $arg->foo );
   return;
}

1;
⚠️ bestie listen β€” the compiled type libraries are more limited than real type libraries. You can't, for example, do parameterized types with them. sobs.

However, they still offer some cool features like:

Foo->check( $value )     # a few basic methods like this
is_Foo( $value )         # boolean checks
assert_Foo( $value )     # assertions which die
Foo | Bar                # unions!

This way you can write a project with object orientation, roles, method modifiers, type-checked attributes, type-checked signatures, and even coercion, with no non-core dependencies! πŸ”₯ (The tools like Mite and Type::Library::Compiler are only needed by the developer, not the end user. main character energy for the dev, chill vibes for the user.)


πŸ‘‘ NEXT STEPS πŸ‘‘

Here's your next step, bestie:

  • Type::Tiny::Manual::UsingWithClassTiny
    • Including how to Type::Tiny in your object's BUILD method, and third-party shims between Type::Tiny and Class::Tiny.

πŸ’œ AUTHOR πŸ’œ

Toby Inkster <tobyink@cpan.org>.


This software is copyright (c) 2022-2025 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


πŸ’€ DISCLAIMER OF WARRANTIES πŸ’€

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

(not me yelling that part in all caps but it's literally how it's written, i don't make the rules πŸ™ˆ)


okay bye bestie, this was perl v5.42.0, dated 2026-01-17, from the Type::T...WithMite(3) page β€” stay hydrated, inline your types, and go compile something cute πŸ’–βœ¨πŸ¦‹πŸŽ€

πŸ“¦ where this page comes from