On 09/12/2018 12:19 AM, Simon Proctor wrote:
In answer to "why the : between Str:D and Cool:D and why Int(Cool:D) ?"
can I just point out the video I linked (or the slides) which answer
both of these questions.
Hi Simon,
Larry Wall, who has a unique gift for making the complex
easy, explained it to me this way:
On 09/12/2018 10:34 AM, Larry Wall wrote:
That is the wrong direction to think about it. Int is not
being redefined as Cool:D there. The basic type there is
just Int, and when the $pos comes in, it will end up being
a simple Int. This syntax is called a "coercion type", and
it just says that we can also accept anything that matches
Cool:D and turn it into an Int. But the inside of the
method knows nothing about the Cool:D part.
Think of Int(Cool:D) as the signature matcher automatically
applying the normal Int($coolthing) coercer for you to
make sure you have an Int, precisely so that you *don't*
have to worry about the Cool:D part inside the routine,
but know that it simply an Int for any part of the routine
after the signature.
multi method contains(Str:D: Cool:D $needle, Int(Cool:D) $pos) --> Bool
$ p6 'say "abc234.56".contains( 2, "1" );' # Str changed to an Int
True
$ p6 'say "abc234.56".contains( 2, 2.4 );' # Real changed to an Int
True