On Wed, 14 Mar 2018 14:16:06 -0700, comdog wrote: > I was playing with coercion types and wondered what would happen if > a .Int method did not return the right sort of type: > > class Foo { > method Int ( --> Str ) { 'Hello' } > } > > put try-it( Foo.new ); > > sub try-it ( Int() $n ) { "Got <$n> of type <{$n.^name}>" } > > Although the subroutine signature demanded an Int, it accepted > something that claimed to be able to convert but actually didn't: > > Got <Hello> of type <Str> > > I would have expected the runtime constraint to check the ultimate > value against the type and this would have failed. > >
Thanks for the report. We'll likely want to extend the features of the coercers, and include the return type check, but not until 6.e or later language versions, when we have some headroom with respect to performance. I've documented[^1] that no check is currently performed on the result and there are some extra proposed spec[^2] for coercers marked for 6.d review already, so I'll mark this ticket as resolved. (though given 6.d is on already the horizon, the check of return type would likely not make it into 6.d). The other feature we'd want to eventually implement is coercing via `Target.new(Source)`, if not `Source.Target` method exists. I've tried[^3] implementation[^4] of checking the final result in January and it dropped performance of coercers by 8%. Given the marginal benefit offered by the fix, we just can't afford to spend a 8% drop in a common feature at the moment. So currently, it's just a "verbal" contract that method Foo returns object Foo, similar how to `sub _foo` is a private method in Perl 5 by convention, despite it being public. [1] https://github.com/perl6/doc/commit/ee34834195 [2] https://github.com/perl6/roast/blob/master/S12-coercion/coercion-types.t [3] https://irclog.perlgeek.de/perl6-dev/2018-01-07#i_15661357 [4] https://gist.github.com/zoffixznet/6d0a2085bd343535b99f548d15547729