What does "!" mean in scala?

scala

Solution

Scala doesn't have operators at the syntax level. All operations are methods.

For example, there is no add operator in the syntax, but numbers have a `+` method:

2.+(3)   // result is 5

When you write `2 + 3`, that's actually syntax sugar for the expression above.

Any type can define a `unary_!` method, which is what `!something` gets desugared to. Booleans implement it, with the obvious meaning of logical negation ("not") that the exclamation mark has in other languages with C heritage.

In your question, the expression is an abbreviated form of the following call:

graph.vertices.filter { t => !(t._2._1) }

where `t` is a tuple-of-tuples, for which the first element of the second element has a type that implements `unary_!` and (as required by `.filter`) returns a `Boolean`. I would bet the money in my pocket that the element itself is a `Boolean`, in which case `!` just means "not."

Problem

I am looking at a piece of code with the following: `graph.vertices.filter(!_._2._1)` I understand that `_` are wildcard characters in `scala` but I do not know what the `!` is supposed to do. What does `!` mean in scala?

Original source