Skip Menu |
Report information
Id: 132980
Status: open
Priority: 0/
Queue: perl6

Owner: Nobody
Requestors: comdog <brian.d.foy [at] gmail.com>
Cc:
AdminCc:

Severity: (no value)
Tag: (no value)
Platform: (no value)
Patch Status: (no value)
VM: (no value)



Date: Wed, 14 Mar 2018 17:15:20 -0400
From: brian d foy <brian.d.foy [...] gmail.com>
Subject: Coercion type apparently does not check the actual type of the coerced value
To: rakudobug [...] perl.org
Download (untitled) / with headers
text/plain 635b
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. -- brian d foy <brian.d.foy@gmail.com> http://www.pair.com/~comdog/
RT-Send-CC: perl6-compiler [...] perl.org
Download (untitled) / with headers
text/plain 1.9k
On Wed, 14 Mar 2018 14:16:06 -0700, comdog wrote: Show quoted text
> 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
RT-Send-CC: perl6-compiler [...] perl.org
Download (untitled) / with headers
text/plain 263b
Re-opening this, as there's a much more common case where a typecheck of the coercion result would be desired and that's when coercion results in a Failure: <Zoffix_> m: multi sub foo(Int() $) { }; foo Inf <camelia> rakudo-moar 5a6ff4073: ( no output )


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

For issues related to this RT instance (aka "perlbug"), please contact perlbug-admin at perl.org