Problem-solving: Use of literal named parameters syntax for multi-dispatching

Created on 16 May 2020  路  3Comments  路  Source: Raku/problem-solving

For now, to distinguish 'foo(:named)' call from foo(:!named) or foo() the most appropriate construct is:

multi foo(:$named! where ? *) { ... }
multi foo(:$named! where ! *) { ... }
multi foo() { ... }

Which is performance-wise sub-optimal and not the easiest code to read.

language

Most helpful comment

Here is a sample with positional:

multi foo('foo') { say "foo:foo" }
multi foo('bar') { say "foo:bar" }
foo(<foo>) # foo:foo

The example from the first comment works like this:

multi foo(:$named! where ? *) { say "foo :named" }
multi foo(:$named! where ! *) { say "foo :!named" }
multi foo() { say "foo" }
foo(:named);  # foo :named
foo(:!named); # foo :!named
foo();        # foo

But it'd be way more readable to have it like this:

multi foo(:named) { say "foo :named" }
multi foo(:!named) { say "foo :!named" }
multi foo() { say "foo" }
foo(:named);  # foo :named
foo(:!named); # foo :!named
foo();        # foo

So, my proposal bottom line is:

  • :named in a signature stands for required named parameter with a true value
  • :!named respectively stands for a required named parameter with a false value

To my view this should improve both readability and simplify optimization.

All 3 comments

Since we already have literal candidate support for positionals, why can't we have it for nameds?

multi foo(:named) { ... }
multi foo(:!named) { ... }
multi foo() { ... }

I'm not entirely clear on what you mean. Could you elaborate?

Here is a sample with positional:

multi foo('foo') { say "foo:foo" }
multi foo('bar') { say "foo:bar" }
foo(<foo>) # foo:foo

The example from the first comment works like this:

multi foo(:$named! where ? *) { say "foo :named" }
multi foo(:$named! where ! *) { say "foo :!named" }
multi foo() { say "foo" }
foo(:named);  # foo :named
foo(:!named); # foo :!named
foo();        # foo

But it'd be way more readable to have it like this:

multi foo(:named) { say "foo :named" }
multi foo(:!named) { say "foo :!named" }
multi foo() { say "foo" }
foo(:named);  # foo :named
foo(:!named); # foo :!named
foo();        # foo

So, my proposal bottom line is:

  • :named in a signature stands for required named parameter with a true value
  • :!named respectively stands for a required named parameter with a false value

To my view this should improve both readability and simplify optimization.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stmuk picture stmuk  路  12Comments

ToddAndMargo picture ToddAndMargo  路  11Comments

ash picture ash  路  13Comments

AlexDaniel picture AlexDaniel  路  10Comments

JJ picture JJ  路  12Comments