2 package Moose::Meta::TypeConstraint::Union;
10 __PACKAGE__->meta->add_attribute('type_constraints' => (
11 accessor => 'type_constraints',
17 my $self = $class->meta->new_object(@_);
21 sub name { join ' | ' => map { $_->name } @{$_[0]->type_constraints} }
24 # this should probably never be used
25 # but we include it here for completeness
28 sub { $self->check($_[0]) };
31 # conform to the TypeConstraint API
37 # not sure what this should actually do here
38 sub coercion { undef }
40 # this should probably be memoized
43 foreach my $type (@{$self->type_constraints}) {
44 return 1 if $type->has_coercion
50 # this feels too simple, and may not always DWIM
51 # correctly, especially in the presence of
52 # close subtype relationships, however it should
53 # work for a fair percentage of the use cases
57 foreach my $type (@{$self->type_constraints}) {
58 if ($type->has_coercion) {
59 my $temp = $type->coerce($value);
60 return $temp if $self->check($temp);
66 sub _compiled_type_constraint {
70 foreach my $type (@{$self->type_constraints}) {
71 return 1 if $type->check($value);
80 $self->_compiled_type_constraint->($value);
87 foreach my $type (@{$self->type_constraints}) {
88 my $err = $type->validate($value);
89 return unless defined $err;
90 $message .= ($message ? ' and ' : '') . $err
93 return ($message . ' in (' . $self->name . ')') ;
97 my ($self, $type_name) = @_;
98 foreach my $type (@{$self->type_constraints}) {
99 return 1 if $type->is_a_type_of($type_name);
105 my ($self, $type_name) = @_;
106 foreach my $type (@{$self->type_constraints}) {
107 return 1 if $type->is_subtype_of($type_name);