4 ms·
Scala solves this with a syntactic twist: If the (in this case usually symbolic) method name ends in a colon (like say `+:`) it's right associative. So the fol
by still_grokking 4y ago
Scala solves this with a syntactic twist: If the (in this case usually symbolic) method name ends in a colon (like say `+:`) it's right associative.
So the following code¹ shows a method call (`prepend`) on the right operand:
val someList = List(2, 3, 4)
val extendedList = 1 +: someList
// As the later is just syntax for a regular method call
// `extendedList` could be also defined with the same result as:
// someList.+:(1)
val otherExtendedList = someList.+:(1)
@main def entryPoint =
println(extendedList)
println(otherExtendedList)
___
¹ https://scastie.scala-lang.org/y55n254oSVOFmw2CjmckjA https://scastie.scala-lang.org/y55n254oSVOFmw2CjmckjA
- eru 4y agoIn Haskell, you can just declare which way your operators are supposed to associate (if any). That doesn't seem to cause any problems in practice. (Though it helps that Haskell is statically typed, so the compiler can yell at you when you guess the direction of associativity for eg your extend-a-list operator wrong.