3

I am wondering what this scala symbol is: _@.

(Search engines have trouble with weird characters so it's hard to find anything on google...)

Here is the context:

def doNodeParse(json: JValue): TreeNode = {
    json match {
        case JObject(List(JField("Condition", JObject(List(JField("var", JString(variableName)), JField("Operation", JString("LT")), JField("Value", JDouble(threshold))))),
                JField("onTrue", _@ onTrue),
                JField("onFalse", _@ onFalse),
                JField("onMissing", _@ onMissing)
                )) =>
                LessThanNode(variableName, threshold, doNodeParse(onTrue), doNodeParse(onFalse), doNodeParse(onMissing))

        case _ => {
            throw new Error("failed parsing json!")
          }
    }
}

(The types of onTrue, onFalse, onMissing are JsonAST.JValue)

anthonybell
  • 5,790
  • 7
  • 42
  • 60

1 Answers1

3

It's legal to omit the space between _ and @ in a pattern match, so in this case it's the same as

    case JObject(List(JField("Condition", JObject(List(JField("var", JString(variableName)), JField("Operation", JString("LT")), JField("Value", JDouble(threshold))))),
            JField("onTrue", _ @ onTrue),
            JField("onFalse", _ @ onFalse),
            JField("onMissing", _ @ onMissing)
            )) =>
            LessThanNode(variableName, threshold, doNodeParse(onTrue), doNodeParse(onFalse), doNodeParse(onMissing))

The effect of the @ operator is to alias the value matched on the left to the name on the right for the match.

Levi Ramsey
  • 18,884
  • 1
  • 16
  • 30
  • Oh, it's in a pattern match! The line was soooooooo long I thought it was in a method call. – pedrofurla Sep 18 '17 at 19:20
  • 2
    so is `_` a variable being thrown away then? I am confused why you would make an alias just to throw it away... why not just do ` JField("onTrue", onTrue)` instead? – anthonybell Sep 18 '17 at 19:20
  • 1
    The normalized form is actually the other way around, `onTrue @ _` etc, that is, what you see in `-Xprint:typer`. I use it to disable `-Ywarn-unused:patvars` – som-snytt Sep 18 '17 at 20:53