On Mon, 24 Jul 2017 10:04:54 -0700, [email protected] wrote:
> The coercion works fine here:
>
> 17:03 Zoffix m: class B {…}; class A { method B { B.new }}; class B
> {}; sub foo(B() $b) { say "hi" }; foo(A.new)
> 17:03 camelia rakudo-moar 2fb8c7: OUTPUT: «hi?»
>
> But if I add `:D` smiley, it fails:
>
> 17:03 Zoffix m: class B {…}; class A { method B { B.new }}; class B
> {}; sub foo(B:D() $b) { say "hi" }; foo(A.new)
> 17:03 camelia rakudo-moar 2fb8c7: OUTPUT: «Type check failed
> in binding to parameter '$b'; expected B but got A (A.new)? in sub
> foo at <tmp> line 1? in block <unit> at <tmp> line 1??»
>
> https://irclog.perlgeek.de/perl6/2017-07-24#i_14915397
Seems to be more generic than custom classes:
$ perl6 -e 'sub foo(Int() $b) { say $b }; foo("42")'
42
$ perl6 -e 'sub foo(Int:D() $b) { say $b }; foo("42")'
Type check failed in binding to parameter '$b'; expected Int but got Str ("42")
in sub foo at -e line 1
in block <unit> at -e line 1
This raises the spec question, should e.g. IO:U($a) try to convert $a to an IO,
and if that succeeds, succeed the bind with the type object of whatever doer (or
subclass were IO a class) of IO which resulted? Or fail the bind? The latter
is
more consistent with the above desired behavior... but does "Class:D()" mean
coerce-to-then-check-definedness, or go-all-out-to-make-a-defined-value?
This doesn't make much sense yet either:
$ perl6 -e 'Int:D(4).say'
(Int:D)