Skip to main content
Post Made Community Wiki by Khelben
Source Link
Alex R
  • 331
  • 1
  • 4

Since this feature is pretty trivial to implement, why isn't it more common?

It isn't trivial to implement (unless trivially implemented). It also doesn't get you very much, even if implemented ideally: the readability gains from terseness are offset by the readability losses from unfamiliarity and opacity. In short, it's uncommon because it's not usually worth the developers' or the users' time.

That said, I can think of three languages that do it, and they do it in different ways:

  • Racket, a scheme, when it isn't being all S-expression-y allows and expects you to write a what amounts to parser for any syntax you want to extend (and provides useful hooks to make this tractable).
  • Haskell, a purely function programming language, allows defining any operator that consists solely of punctuation, and allows you to provide a fixity level (10 available) and an associativity. Ternary etc. operators can be created out of binary operators and higher order functions.
  • Agda, a dependently typed programming language, is extremely flexible with operators (paper here) allowing both if-then and if-then-else to be defined as operators in the same program, but its lexer, parser, and evaluator are all strongly coupled as a result.