Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "!" mean in scala?

Tags:

scala

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?

like image 319
LearningSlowly Avatar asked Oct 25 '16 19:10

LearningSlowly


People also ask

What does _ _1 mean in Scala?

_1 is a method name. Specifically tuples have a method named _1 , which returns the first element of the tuple.

What does ::: mean in Scala?

The ::: operator in Scala is used to concatenate two or more lists. Then it returns the concatenated list. Example 1: Scala.

What does () mean in Scala?

It can denote a function or method that takes no parameters, such as: def foo() = "Hello world" Note that when writing an anonymous function, the () is by itself but still means a function with no parameters.

What is the use of symbol in Scala?

Symbols are used to establish bindings between a name and the entity it refers to, such as a class or a method. Anything you define and can give a name to in Scala has an associated symbol.


1 Answers

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."

like image 158
Roberto Bonvallet Avatar answered Oct 21 '22 07:10

Roberto Bonvallet