Search code examples
scalainfix-notation

What do these Scala statements mean? and what do you call this syntax? [infix notation]


I am trying to figure out some Scala code. Here's a snippet that creates an mutable Map and Ordering object.

import scala.collection.mutable

val score = mutable.Map(start -> 0d) withDefaultValue Double.PositiveInfinity
val priority = Ordering by {n: Node => score(n) + heuristic(n)}

What do you call this code where you just put terms after an expression without commas or parenthesis? I have a feeling this is the functional aspect of Scala but not sure. Do the curly brackets have a different meaning in this context?


Solution

  • What do you call this code where you just put terms after an expression without commas or parenthesis?

    This is using 'infix notation', see http://docs.scala-lang.org/style/method-invocation.html

    I have a feeling this is the functional aspect of Scala but not sure.

    This is 'syntactic sugar' and not directly related to functional programming.

    Do the curly brackets have a different meaning in this context?

    In this context, curly braces define a expression block. More detailed discussion available here: What is the formal difference in Scala between braces and parentheses, and when should they be used?