Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the MooseX-CoercePerAttribute CPAN distribution.

Report information
The Basics
Id: 83573
Status: resolved
Priority: 0/
Queue: MooseX-CoercePerAttribute

People
Owner: MRF [...] cpan.org
Requestors: perl [...] toby.ink
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in:
  • 0.802
  • 0.801
Fixed in: (no value)



Subject: Use of hashref to declare coercions is problematic
Given the following type definition: subtype "SmallInt", as "Int", where { $_ < 10 }; coerce "SmallInt", from Num => via { 1 }, from Str => via { 2 }; The value "33.333" will be coerced to "1". But if you define the type this way: subtype "SmallInt", as "Int", where { $_ < 10 }; coerce "SmallInt", from Str => via { 2 }, from Num => via { 1 }; Then "33.333" will be coerced to "2". It matters what order you define the coercions in. The earlier one "wins". If you define a per-attribute coercion like this: has small => ( traits => [ "CoercePerAttribute" ], is => "ro", isa => "SmallInt", coerce => { Num => sub { 1 }, Str => sub { 2 }, }, ); there's no way of knowing whether the Num coercion will win, or Str will, because hashes are unordered in Perl.
Right. It seems that Moose type Coercions expect an list of the from => via to be used. I will look at implementing the change to use a list rather than a hash. I will likely support hash format for a while with a possible warning to switch to a list format. Should be able to have a fix out in the next few weeks. Cheers, MRF On Sat Feb 23 13:52:06 2013, TOBYINK wrote: Show quoted text
> Given the following type definition: > > subtype "SmallInt", as "Int", where { $_ < 10 }; > coerce "SmallInt", > from Num => via { 1 }, > from Str => via { 2 }; > > The value "33.333" will be coerced to "1". > > But if you define the type this way: > > subtype "SmallInt", as "Int", where { $_ < 10 }; > coerce "SmallInt", > from Str => via { 2 }, > from Num => via { 1 }; > > Then "33.333" will be coerced to "2". > > It matters what order you define the coercions in. The earlier one > "wins". > > If you define a per-attribute coercion like this: > > has small => ( > traits => [ "CoercePerAttribute" ], > is => "ro", > isa => "SmallInt", > coerce => { > Num => sub { 1 }, > Str => sub { 2 }, > }, > ); > > there's no way of knowing whether the Num coercion will win, or Str > will, because hashes are unordered in Perl.