0

I am reading some Scala code. What does the -> mean in the following context?

var queries = { "Select apple from farm" -> None, "Select orange from fram" -> None, "Select blueberry from anotherFarm" -> Some( #randomStuff ) }

It looks like a list of lambda functions but I thought it should be => in that case instead of ->.

Also, what does this single line code mean?

def onConnection(id) = { application ! turnOnApplication(id) }

Specifically, I am confused with the use of !. It doesn't seem to be a "NOT" as it is in most languages

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
H. Zeng
  • 47
  • 6
  • You should seperate your questions and ask two different ones. – Luka Jacobowitz Jun 12 '17 at 14:11
  • I can only post every 90 minutes. I changed the title. They are simple syntax questions so I think it's fine to be put together – H. Zeng Jun 12 '17 at 14:19
  • 1
    https://stackoverflow.com/questions/17644273/akka-in-scala-exclamation-mark-and-question-mark https://stackoverflow.com/questions/40248204/what-does-mean-in-scala/40248896#40248896 https://stackoverflow.com/questions/17307472/understanding-scala-syntax – Bergi Jun 12 '17 at 14:55
  • Where did you get the first one from? That does not look like valid syntax. – sepp2k Jan 31 '18 at 18:33

4 Answers4

5

The -> symbol is one way to define a tuple in Scala. The below are all equivalent:

val apples1 = "Select apple from farm" -> None
val apples2 = ("Select apple from farm" -> None)
val apples3 = ("Select apple from farm", None)

As for the !:

def onConnection(id) = { application ! turnOnApplication(id) }

! in Scala can be the negation operator, but ! in the above code snippet looks like tell from Akka (Akka is the main actor library for Scala). This pattern is used to send a message to an actor. So if application is a reference to an actor, the code snippet sends the result of turnOnApplication(id) to the application actor. From the linked documentation:

"!" means “fire-and-forget”, e.g. send a message asynchronously and return immediately. Also known as tell.

Roberto Bonvallet
  • 31,943
  • 5
  • 40
  • 57
Jeffrey Chung
  • 19,319
  • 8
  • 34
  • 54
1

The thin arrow -> is Tuple syntax. It's just a different way of writing Tuples. I.e.

val x: (Int, String) = 3 -> "abc" 

Is the same as writing:

val x: (Int, String) = (3, "abc")

The arrow syntax is done by providing an implicit class ArrowAssoc which defines a method def ->[B](y: B): (A, B). ArrowAssoc is part of Predef which is inserted into every Scala source file. You can find the docs here.

The bracket syntax meanwhile is syntactic sugar done by the compiler.

Luka Jacobowitz
  • 22,795
  • 5
  • 39
  • 57
0

You can form tuple using two syntaxes

1) Using comma

val tuple = (1, 2)

2) Using -> (arrow)

val tuple = 1 -> 2

Scala repl

scala> val tuple = (1, 2)
tuple: (Int, Int) = (1,2)

scala> val tuple = 1 -> 2
tuple: (Int, Int) = (1,2)
Nagarjuna Pamu
  • 14,737
  • 3
  • 22
  • 40
0

Finding-symbols defines -> as Method provided by implicit conversion. Just look at the methods tagged with implicit that receive, as parameter, an object of type that is receiving the method. For example:

"a" -> 1  // Look for an implicit from String, AnyRef, Any or type parameter

In the above case, -> is defined in the class ArrowAssoc through the method any2ArrowAssoc that takes an object of type A, where A is an unbounded type parameter to the same method.

tutorialPoint definde ! as It is called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.

Ramesh Maharjan
  • 41,071
  • 6
  • 69
  • 97