j1n3l0 has asked for the wisdom of the Perl Monks concerning the following question:

Hello venerable monks,

I have been playing around with rakudo and have a few questions demonstrated in the following piece of code:

multi sub to_num(Str $number where { $_ ~~ /^ \d+ $/ }) returns Num { ++$number } my $s = "10"; my $i = to_num($s); say 'before: ' ~ $s.WHAT; # before: Str say 'after : ' ~ $i.WHAT; # after : Num

Questions:

Casting

Is it currently possible to convert a type Str "10" to a type Num 10 e.g. the return value of the builtin prompt()?

Return types

Should a sub/method/function complain if the return value does not match the specified type, in this case Num?

Signatures

Is the return type part of the signature and if not should it be?

Thanks =)


Smoothie, smoothie, hundre prosent naturlig!

Replies are listed 'Best First'.
Re: casting, signatures and return types in perl 6 (rakudo)
by moritz (Cardinal) on Mar 17, 2009 at 14:42 UTC
    Type coercions are specced to work with as Num in the signature, but it's not yet implemented in Rakudo.

    If you want to modify a builtin function in that way, the specification says (in Wrapping) you can do something like this: &prompt.wrap( { +callsame } )

    but I fear it's not implemented either. For now you'll just have to write +prompt.

    Return types Should a sub/method/function complain if the return value does not match the specified type, in this case Num?

    It should. But it's not yet implemented.

    Is the return type part of the signature and if not should it be?

    Yes, it should appear as :(rest of signature... --> Num)

    Let me add that signatures are only implemented in Rakudo to the amount that's needed to do multi dispatch, and return values and coercion of return values don't count among them. (and, of course, patches welcome, both to the test suite and the implementation :-)

      Thanks for the clarification. I will contribute any patches I think are meaningful ;)


      Smoothie, smoothie, hundre prosent naturlig!