in reply to Moose subtypes

Hi K.D.J.

Here's how I do it in Moo. The Type::Tiny libs work seamlessly with Moose, I believe, so this code should Moosify as is.

package My::Types; use strict; use warnings; use parent 'Type::Library'; use Type::Utils; # gets you sugar, 'declare' etc. use Types::Standard qw/ Maybe Str Int /; declare 'OptInt', as Maybe[Int], message {'must be integer or nothing' +}; declare 'OptStr', as Maybe[Str], message {'must be string or nothing'} +; 1;
package My::Class; use Moo; use My::Types qw/ :all /; has optional_numeric_arg => ( is => 'ro', isa => OptInt ); has optional_string_arg => ( is => 'ro', isa => OptStr ); 1;

Note that as an added benefit this also gets you an importable is_TypeName function for each type you declare that can be used anywhere for simple data validation.

( I believe I first learned of these techniques in This Advent Calendar article. )

Hope this helps...

The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Moose subtypes
by kdjantzen (Acolyte) on Oct 09, 2016 at 19:21 UTC
    Hello 1nickt,

    thanks for the very helpful information.
    Using it I got my code working as I wanted it.