Skip Menu |

This queue is for tickets about the Moose CPAN distribution.

Report information
The Basics
Id: 58387
Status: resolved
Priority: 0/
Queue: Moose

People
Owner: stevan.little [...] gmail.com
Requestors: IKEGAMI [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Maybe[] breaks coerce=>1
Maybe[] breaks coerce=>1. The following is a test script that demonstrates the problem: ===== BEGIN TEST CODE ===== BEGIN { package Class; use strict; use warnings; use Moose; has val => ( is => 'rw', isa => 'Num', required => 1, ); use Moose::Util::TypeConstraints; coerce __PACKAGE__ => from 'Num' => via { __PACKAGE__->new(val => $_) }; } BEGIN { package Container; use strict; use warnings; use Moose; has always => ( is => 'rw', isa => 'Class', coerce => 1, default => sub { Class->new(val => 0) }, ); has maybe => ( is => 'rw', isa => 'Maybe[Class]', coerce => 1, default => undef, ); } use strict; use warnings; use Test::More tests => 15; my $always_fail = qr/^Attribute \(always\) does not pass the type constraint/; my $maybe_fail = qr/^Attribute \(maybe\) does not pass the type constraint/; my $o = Class->new(val => 123); eval { Container->new() }; is($@, ''); eval { Container->new(always => $o) }; is($@, ''); eval { Container->new(always => 123) }; is($@, ''); eval { Container->new(always => "abc") }; like($@, $always_fail); eval { Container->new(maybe => undef) }; is($@, ''); eval { Container->new(maybe => $o) }; is($@, ''); eval { Container->new(maybe => 123) }; is($@, ''); eval { Container->new(maybe => "abc") }; like($@, $maybe_fail); my $c = Container->new(); eval { $c->always($o) }; is($@, ''); eval { $c->always(123) }; is($@, ''); eval { $c->always("abc") }; like($@, $always_fail); eval { $c->maybe(undef) }; is($@, ''); eval { $c->maybe($o) }; is($@, ''); eval { $c->maybe(123) }; is($@, ''); eval { $c->maybe("abc") }; like($@, $maybe_fail); ===== END TEST CODE ===== (Workaround: http://www.perlmonks.org/?node_id=843554)
This is working as intended. Coercions are attached to the actual type, and Maybe[Foo] is a different type from Foo. Allowing "deep coercions" which propagate into parameterized constraints has been requested on several occasions, but it's generally considered to be too "magical" for core behavior. It would probably be a useful extension though.
Actually, Maybe[Foo] and Foo are two different types and Maybe[`a] (like all parameterized types) does not inherit the coercions of the type it wraps (meaning, no deep coercion). I actually responded to the perlmonks thread as well. In which I suggest that the OP look into MooseX::UndefTolerant which takes a more liberal view of undefined values then the core Moose. Just as a note, it is possible to subtype a Maybe[`a] type, so the OP could do something like: subtype 'MaybeDateTime' => as 'Maybe[DateTime]'; And then define the coercions on the 'MaybeDateTime' type itself, which is another possible way to approach solving the OPs issue. In short, this is not a bug and the topic of deep coercion has been discussed at length and has been decided to be a bad thing. If you disagree and wish to debate the issue, I suggest the Moose mailing list. - Stevan
On Mon Jun 14 17:10:01 2010, STEVAN wrote: Show quoted text
> Maybe[`a] does not inherit the coercions of the type it wraps > (meaning, no deep coercion).
Yes, that's the bug I'm reporting. Show quoted text
> In short, this is not a bug and the topic of deep coercion has been > discussed at length
Link?
On Mon Jun 14 17:52:17 2010, ikegami wrote: Show quoted text
> On Mon Jun 14 17:10:01 2010, STEVAN wrote:
> > Maybe[`a] does not inherit the coercions of the type it wraps > > (meaning, no deep coercion).
> > Yes, that's the bug I'm reporting.
That is not a bug, it is a feature request, and as I said before, it will not happen in core Moose. Show quoted text
> > In short, this is not a bug and the topic of deep coercion has been > > discussed at length
> > Link?
You can try digging in the mailing list archives but the bulk of the conversation took place on IRC and so I cannot provide a link, sorry. Sorry, but if you really want this feature it will have to be a MooseX:: module, it simply will not be added into the core. - Stevan
On Mon Jun 14 19:19:51 2010, STEVAN wrote: Show quoted text
> On Mon Jun 14 17:52:17 2010, ikegami wrote:
> > On Mon Jun 14 17:10:01 2010, STEVAN wrote:
> > > Maybe[`a] does not inherit the coercions of the type it wraps > > > (meaning, no deep coercion).
> > > > Yes, that's the bug I'm reporting.
> > That is not a bug, it is a feature request, and as I said before, it > will not happen in core Moose. >
> > > In short, this is not a bug and the topic of deep coercion has
> been
> > > discussed at length
> > > > Link?
> > You can try digging in the mailing list archives but the bulk of the > conversation took place on IRC > and so I cannot provide a link, sorry. > > Sorry, but if you really want this feature it will have to be a > MooseX:: module, it simply will not be > added into the core. > > - Stevan > > > > > > > > >
CC: IKEGAMI [...] cpan.org
Subject: Re: [rt.cpan.org #58387] Maybe[] breaks coerce=>1
Date: Mon, 14 Jun 2010 21:23:46 -0400
To: bug-Moose [...] rt.cpan.org
From: Eric Brine <ikegami [...] adaelis.com>
On Mon, Jun 14, 2010 at 7:19 PM, Stevan Little via RT <bug-Moose@rt.cpan.org Show quoted text
> wrote:
Show quoted text
> Sorry, but if you really want this feature it will have to be a MooseX:: > module, it simply will not be > added into the core. >
I have no problem with that. But apparently, there are dragon lurking I should be familiar with.
On Mon Jun 14 21:23:55 2010, ikegami@adaelis.com wrote: Show quoted text
> On Mon, Jun 14, 2010 at 7:19 PM, Stevan Little via RT <bug-Moose@rt.cpan.org
> > wrote:
>
> > Sorry, but if you really want this feature it will have to be a MooseX:: > > module, it simply will not be > > added into the core. > >
> > I have no problem with that. But apparently, there are dragon lurking I > should be familiar with.
No, the use of Undef in Moose is internally consistent and has been so for the last 60 or so releases, a lot of work was put in by a lot of people to assure this is true. The behavior of all parameterized types is also consistent and we make every attempt to make sure it fits cleanly into the greater type system. There are no dragons here and if they are, they are quite tame. However, that said, what you need to keep in mind is that the Moose idea of Undef and what it means within the type system is (apparently) not the same as how you view Undef. There is actually a thread going on in the mailing list about this very topic and my latest reply attempts to clarify the Moose view a little more. You can read it here: http://article.gmane.org/gmane.comp.lang.perl.moose/1697 Thanks, - Stevan