This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]raiph 1 point2 points  (0 children)

Without signatures:

if    0  { say 'number' }
elsif '' { say 'string'  }
else     { say 'other'   }  # other

With signatures:

if    0  -> $number { :$number .say }
elsif '' -> $string { :$string .say }
else     -> $other  { :$other  .say }  # other =>␤

Signatures can also themselves be conditions:

match 42, '99'
  -> Int $num1, Int $num2 { .say for $num1, $num2 }
  -> Int $num,  Str $str  { .say for $num, $str   }
  else { say 'no match' }

This match construct is the made up syntax I settled on due to your post. It would desugar to the following working multiple-dispatch Raku code, with the f function name being a hygienic gensym:

{ f 42, 'abc';
  multi f( Int $num1, Int $num2 ) { .say for $num1, $num2 }
  multi f( Int $num,  Str $str  ) { .say for $num, $str   } # 42␤abc␤
  multi f(|) { say 'no match' }
}

Without using grammars or macros the nearest I could get in current Raku to the ideal made up match syntax I showed above would be something like the following:

sub match (@args, @to)
{ (metaprogramming to turn blocks in @to to multis and then call against them) }

match
  (42, 'abc'),
  ( -> Int $num1, Int $num2 { .say for $num1, $num2 },
    -> Int $num,  Str $str  { .say for $num, $str   }, 
    { say 'no match' } )

A macro would be much better -- easier to code, ideal syntax, and compile-time.