Stefan (>):
> A methodical is an operator which syntactically behaves as a method but is
> subject to scoping rules.  Methodicals are defined using the ordinary method
> keyword, qualified with my or our.  (TODO: This seems the most natural syntax
> to me, but it conflicts with existing usage.  Which is more worth having on
> it?)  Methodicals do not need to be declared in classes, but they should
> generally have declared receiver types.
>
>    {
>        my method make-me-a-sandwich(Num:) { ... }
>
>        2.make-me-a-sandwich; # calls our method
>
>        "ham".make-me-a-sandwich; # either an error, or method dispatch
>    }
>
>    2.make-ma-a-sandwich; # ordinary method dispatch

With just the addition of a single character, this already works in Perl 6:

$ perl6 -e 'my &make-me-a-sandwich = method (Numeric $sandwichee:) {
say "Hokay, $sandwichee" }; 2.&make-me-a-sandwich;
"ham".&make-me-a-sandwich'
Hokay, 2
Nominal type check failed for parameter '$sandwichee'; expected
Numeric but got Str instead

It is maybe a testament to Perl 6's "strange consistency" that no-one
had anticipated this combination of syntax and semantics, and yet
there it is, fallen out of more fundamental design choices. There was
some surprise and amusement on #perl6 when we discovered this.

In summary, the .& syntax:

* can be used on lexical variables that don't pollute the global
referencing environment,
* does not compromise encapsulation,
* does not require MONKEY_TYPING,
* does not conflict with the spec'd usage of the 'method' declaration,
* is implemented today, as it turns out.

's all good.

// Carl

Reply via email to