yashpal bharadwaj
yashpal bharadwaj

Reputation: 323

Scala | what is the difference between '<-', '->' and '=>' operators and how do they work implicitly?

While studying basic of Scala I came across "<-" operator which they say is a generator from any range/list or collection.

  1. On what basis it generates the values and what may be the way to do that implicitly?
  2. Is compulsory to take a main() in Object.scala? Can't I define a main() in a Class.scala?
  3. Is compulsory to make an Object.scala to take the output from the methods of a class?

Upvotes: 3

Views: 4771

Answers (1)

Jesper
Jesper

Reputation: 206786

Arrows

<-, -> and => are completely different things with completely different uses in Scala - it doesn't make a lot of sense to compare them.

<- is used in for comprehensions. On the right side of the <- is a generator, which is an instance of a type with a foreach method, which generates the elements that the for is going to loop over. Note that the generator does not need to be a collection - anything that has a foreach method will work (it can for example be an Option).

-> is a method that creates a tuple. This method is often used when creating a Map with a convenient, readable syntax. For example:

val map = Map("one" -> 1, "two" -> 2, "three" -> 3)

Note that "one" -> 1 is the same as ("one", 1) (a Tuple2 containing the values "one" and 1), etc.

=> is used in function literals and function types, it's the separator between the function's (or function type's) arguments and body (or return type).

main()

Scala doesn't have static like Java. Anything that you would make static in Java, you would put in an object instead of in a class in Scala.

Since the main() method should be static, it must be defined in an object in Scala instead of in a class.

Ofcourse you could create a main() method in a class, but you cannot use this as the entry point of an application - it would just be a regular method that happens to be named main.

Upvotes: 10

Related Questions