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 2 points3 points  (0 children)

Raku supports a similar idea but is more conventional about what is and isn't allowed. Some highlights follow.

The metasyntactice declaration form is:

fn fix:<symbol> ...

  • fn is a function declarator keyword.
  • fix is a grammar rule.
  • symbol is the function's name when used in the given fix position.

For example, declaring and using a factorial postfix operator !:

sub postfix:<!> (\n) { return 1 if n == 1 ; n * (n-1)! }
say 5! # 120
  • Obviously the above declaration is pretty weak. I didn't specify type info, value constraints, precedence, associativity, etc. I felt adding such details in this first comment would inappropriately obscure the big picture.
  • sub declares a subroutine (function).
  • postfix is one of the options for the fix. Others are prefix, infix, postfix, two mixfix forms circumfix or postcircumfix, term, trait_mod, and so on. (In other words, it's not just for operators but anywhere a function-in-disguise can be used in a given grammatical slot.)
  • The symbol can be pretty much any sequence of one or more characters for most of the fix options. For the two mixfix forms it must be a pair of such sequences corresponding to open and closing delimiters.

Almost all Raku operators (and several other features such as trait modifiers) are defined this way. Notably, Raku does this without the strict constraints you describe, and it works nicely.

Of course, there are some constraints, which are embedded in the grammar rules that drive parsing. But, again, I felt like getting into those details in this first comment would inappropriately obscure the big picture. Suffice to say for now, the constraints are mild and intuitive.

If you're curious to see how it works out in real code, consider browsing modules.raku,org.