0

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?

swdev
  • 2,941
  • 2
  • 25
  • 37
  • The compiler adjusts (or "de-sugars") `2 + 3` into the more syntactically correct `2.+(3)`. That adjustment can be applied to any type (like `Int`) with a method (like `+`) that takes a single argument (`3` in this example). – jwvh Jun 14 '17 at 22:41

1 Answers1

3

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?

FabFlying
  • 163
  • 7