From: Kent Fredric Date: Sun, 19 Jun 2011 00:11:45 +0000 (+1200) Subject: Add a union type constraint constructor sugar to X-Git-Tag: 2.0103~16 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0faea2a867c4e0d4bfcce3ecef592b234145a070;p=gitmo%2FMoose.git Add a union type constraint constructor sugar to Moose::Util::TypeConstraints --- diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index 4ab0006..9344607 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -41,7 +41,7 @@ Moose::Exporter->setup_import_methods( type subtype class_type role_type maybe_type duck_type as where message optimize_as inline_as coerce from via - enum + enum union find_type_constraint register_type_constraint match_on_type ) @@ -401,6 +401,25 @@ sub enum { ); } +sub union { + my ( $type_name, @constraints ) = @_; + if ( ref $type_name eq 'ARRAY' ) { + @constraints == 0 + || __PACKAGE__->_throw_error("union called with an array reference and additional arguments."); + @constraints = @$type_name; + $type_name = undef; + } + if ( @constraints == 1 && ref $constraints[0] eq 'ARRAY' ) { + @constraints = @{ $constraints[0] }; + } + my $tc = create_type_constraint_union( @constraints ); + if ( defined $type_name ) { + $tc->name( $type_name ); + return register_type_constraint( $tc ); + } + return $tc; +} + sub create_enum_type_constraint { my ( $type_name, $values ) = @_;