in reply to MooseX::Declare and undef attributes - MooseX::UndefTolerant not working

undef isn't a string. If undef is an acceptable value, you want Maybe[Str] or Undef|Str.

Replies are listed 'Best First'.
Re^2: MooseX::Declare and undef attributes - MooseX::UndefTolerant not working
by err (Novice) on Sep 26, 2010 at 12:15 UTC

    I think maybe does the job. For the standard Moose types it works well like isa => 'Maybe[Str]'. The problem is I created some subtypes for my needs.

    package Foo::Types::Library; use Data::Dumper; use MooseX::Types -declare => [ qw/ GooglePrimaryEmail WWWGoogleContactsTypeEmail / ]; use MooseX::Types::Email qw/EmailAddress/; # WWWGoogleContactsTypeEmail class_type WWWGoogleContactsTypeEmail, { class => 'WWW::Google::Contacts::Type::Email', }; # GooglePrimaryEmail subtype GooglePrimaryEmail, as EmailAddress, message { 'validation failed for: '. Dumper $_; }; coerce GooglePrimaryEmail, from ArrayRef[WWWGoogleContactsTypeEmail], via { my $p = (grep { $_->{primary} } @$_)[0]; $p = $p ? $p->{value} : @$_->[0]{value}; return $p; };

    This works for something like:

    use MooseX::Declare; class Foo { use Foo::Types::Library qw /GooglePrimaryEmail/; has 'email' => ( is => 'rw', isa => GooglePrimaryEmail, coerce => 1 ); }

    Works fine unless email is initialized undef. In this case the attribute check fails.
    Now maybe solves this but I don't know how I can create my own Maybe. I use MooseX::Types for creating my own types. As I can't see any syntax to create my own Maybe type I used the one in Moose::Util::TypeConstraints:

    maybe_type (GooglePrimaryEmail);

    I added this line to my type library, exported it with declare as Maybe[GooglePrimaryEmail and tested it with:

    use MooseX::Declare; class Foo { use Foo::Types::Library qw /Maybe[GooglePrimaryEmail]/; has 'email' => ( is => 'rw', isa => 'Maybe[GooglePrimaryEmail]', coerce => 1 ); }

    Now every attribute type check fails. :(

    Any help?

      Maybe[...] doesn't work with coercion (wontfix), but ...|Undef does (although it was buggy before 1.11).

        Ok you faced the same problem. I'll give Undef| a chance. Thanks for help