3 ms·
Raku [1] deserves a honorable mention where it has named anonymous parameters, written as `$^x` and ordered lexicographically. In my taste as a PL enthusiast,
by lifthrasiir 3y ago
Raku [1] deserves a honorable mention where it has named anonymous parameters, written as `$^x` and ordered lexicographically.
In my taste as a PL enthusiast, the biggest problem with this kind of anonymous parameters is a conflict with nested blocks. Suppose that our fictitious language is expression-oriented, `{ |x| ... }` denotes an explicit closure, `{ ... }` denotes an implicit closure and `$1`..`$n` are anonymous parameters in them. Consider the following examples:
{ $1 + $2 } // Yeah, that would be `{ |x, y| x + y }`
{} // Is this a zero argument function?
{ $100 } // Does this have 100 arguments???
{ |x| $1 + $2 } // Maybe this should be invalid...
{ { |x| $1 + $2 } } // But maybe this should be okay??
{ { |x| $1 + x }($2) } // And same to this???
{ { |x| $1 }($2) } // Uh...
{ { $1 }($2) } // Should be same to above, but...
{ { $2 }($1) } // Not this (and will fail)!
The problem is much worse when you don't even have an explicit boundary like `{ ... }`. A limited form of automatic function, say `_ + _` without no parentheses, might work, but anything beyond that quickly devolves into a mess.
[1] https://docs.raku.org/language/variables#Variables https://docs.raku.org/language/variables#Variables
- gpderetta 3y agohttps://en.wikipedia.org/wiki/De_Bruijn_index https://en.wikipedia.org/wiki/De_Bruijn_index
- jzimbel 3y agoElixir puts some guardrails on its numbered-argument anonymous function syntax, which I think do a good job of restricting its use to defining extremely simple functions. 1. The function has to use all of its arguments at least once. You can’t do `&(&2)`, because it skips the first argument. 2. Single-expression. You can’t do something like `&(x = &1; x + 1)`. 3. No nesting. You can’t define an additional anonymous function inside of a & expression, or even use a `&SomeModule.some_fn/arity` capture. For anonymous functions with nesting, you have to use the `fn arg1, arg2, ... -> body end` syntax.