-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Topic/typeconstraint allow string objects #169
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,28 @@ use Moose; | |
extends 'Moose::Exception'; | ||
with 'Moose::Exception::Role::Class'; | ||
|
||
# An indiscriminate `use` without an import list somehow makes | ||
# type_constraint_message a hash memory address | ||
use Moose::Util::TypeConstraints qw( | ||
duck_type | ||
|
||
coerce | ||
from | ||
via | ||
|
||
subtype | ||
as | ||
); | ||
subtype '_MooseImmediateStr' => as 'Str'; | ||
subtype '_MooseDucktypeStr' => as duck_type([qw< ("" >]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a bit more verbose but I think it's better to write this as something like this: my $stringifiable = subtype(
as 'Str',
inline_as { qq[Scalar::Util::blessed( $_[0] ) && defined overload::Method( $_[0], q{""} )] },
); So there's a couple things there:
Note that you also use anon empty subtype of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then I realized I'm the one who suggested coercing to a string in the first place. But was that a good suggestion? I'm not sure. I can see arguments both ways. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for the delay on this (I was in Yosemite for at least part of the delay!) - I can see the argument for both as well. I personally would prefer to just pass the object through, but I think we're worried about backwards compatibility, correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @karenetheridge - what are your thoughts here? I would argue that returning a stringifiable object is fine as a change for a major release. |
||
coerce '_MooseImmediateStr', | ||
from '_MooseDucktypeStr', | ||
via { "$_" }; | ||
|
||
has 'type_constraint_message' => ( | ||
is => 'ro', | ||
isa => 'Str', | ||
isa => '_MooseImmediateStr', | ||
coerce => 1, | ||
required => 1 | ||
); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use strict; | ||
use warnings; | ||
|
||
# RT 123299 | ||
|
||
use Test::More; | ||
use Test::Fatal; | ||
|
||
{ | ||
package String::Object; | ||
|
||
use overload '""' => \&as_string, fallback => 1; | ||
|
||
sub new { | ||
my $self = bless {}, __PACKAGE__; | ||
} | ||
|
||
sub as_string { 'successful exception' } | ||
|
||
package Bad::Moose::Types; | ||
|
||
use Moose; | ||
use MooseX::Types::Moose qw( Str ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't use MX::Types in Moose tests except as an optional dep, but I don't think you need to use MX::Types for this anyway. |
||
use MooseX::Types -declare => [ 'AlwaysFail', 'StringObject' ]; | ||
|
||
subtype AlwaysFail, | ||
as Str, | ||
where { 0 }, | ||
message { String::Object->new }; | ||
|
||
class_type StringObject, { class => 'String::Object' }; | ||
coerce Str, from StringObject, via { 'coerced' }; | ||
|
||
package Bad::Moose; | ||
use Moose; | ||
|
||
has validation_fail => ( | ||
is => 'ro', | ||
isa => Bad::Moose::Types::AlwaysFail, | ||
); | ||
|
||
__PACKAGE__->meta->make_immutable; # This is the key line | ||
} | ||
|
||
package main; | ||
|
||
like( | ||
exception { Bad::Moose->new( validation_fail => '123' ) }, | ||
qr/successful exception/, | ||
"Moose type constraints accept stringifiable type constraint errors" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a little confusing. I think this would be better as |
||
); | ||
|
||
done_testing(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is very pretty, but we don't do this anywhere else in the code base, so I think it's better to just format this like everything else.