On 6/23/06, Steffen Schwigon <[EMAIL PROTECTED]> wrote:
Steffen Schwigon <[EMAIL PROTECTED]> writes:
>   multi sub talk () { say 'Loose Talk Is Noose Talk.'; }
>   multi sub talk (String $msg) { say $msg; }
>   multi sub talk (String $msg, Int $times) { say $msg x $times; }

BTW, because we are just on-topic, can someone explain, when these
types above are used. They seem pretty useless in my example but it
looked that nice. :-)

IMHO they are already used in that example automatically. You just
don't see it because every multi sub had different amount of
parameters.
This example might make it a bit clearer:

multi sub talk (String $msg1, String $msg2) { say "$msg1 $msg2" }
multi sub talk (String $msg, Int $times) { say $msg x $times; }
multi sub talk (String $msg, Num $times) { say "Please use an integer"; }
multi sub talk (String $msg, Range $r) { say "$_: $msg" for $r }

talk("Hello", "World"); # String and String
talk("Hi", 2); # String and Int
talk("Test", 1.23); # String and Num
talk("Hello", 3..5); # String and Range
talk(123, 3); # Int and Int

I think that would print
 Hello World
 HiHi
 Please use an integer
 3: Hello
 4: Hello
 5: Hello
 123123123

In last example, there is no exact match for parameters (Int, Int), so
IMHO perl6 would select the closest match, which in this case is
(String, Int) because Int can be converted to String.

The Range-example is my quess of how Range could be used with "for".
I'm not 100% sure if that syntax is OK.


How do I extend the example to really check the parameter types. Some
kind of "use strict" anywhere?

I think that parameter types are automatically checked.
Also "use strict;" is default in perl6, but that's a bit different thing IMHO.

--
Markus Laire

Reply via email to