Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nine ways to define a method in Scala?

So I've been trying to puzzle through the various ways you can define stuff in Scala, complicated by my lack of understanding of the way {} blocks are treated:

object NewMain extends Thing{      def f1 = 10     def f2 {10}     def f3 = {10}     def f4() = 10     def f5() {10}     def f6() = {10}     def f7 = () => 10     def f8 = () => {10}     def f9 = {() => {10}}      def main(args: Array[String]){         println(f1)     // 10         println(f2)     // ()         println(f3)     // 10         println(f4)     // 10         println(f4())   // 10         println(f5)     // ()         println(f5())   // ()         println(f6)     // 10         println(f6())   // 10         println(f7)     // <function0>         println(f7())   // 10         println(f8)     // <function0>         println(f8())   // 10         println(f9)     // <function0>         println(f9())   // 10     }  } 

Presumably some of these are equivalent, some of these are syntactic sugar for others, and some are things I should not use, but I can't for the life of me figure it out. My specific questions are:

  • How is it that println(f2) and println(f5()) gives unit? Isn't the last item in the block 10? How is it different from println(f3()), which gives 10?

  • If println(f5) gives unit, shouldn't println(f5()) be invalid, since unit is not a function? The same applies to println(f6) and println(f6())

  • Of all the ones which print 10: f1, f3, f4, f4(), f6, f6(), f7(), f8(), f9(), is there any functional difference between them (in terms of what it does) or usage differences (in terms of when I should use which)? Or are they all equivalent?

like image 582
Li Haoyi Avatar asked Nov 28 '11 23:11

Li Haoyi


People also ask

How do you define a method in Scala?

Scala doesn't allow us to define an anonymous method. We can define a method's list of parameters in parentheses after its name. After the parameters list, we can provide a return type for the method with the leading colon sign. We then define a method's body after the equals sign.

What are the method parameters in in Scala?

Methods in Scala can be parameterized by type as well as value. The syntax is similar to that of generic classes. Type parameters are enclosed in square brackets, while value parameters are enclosed in parentheses. The method listOfDuplicates takes a type parameter A and value parameters x and length .

How do you create a function in Scala?

In scala, functions are first class values. You can store function value, pass function as an argument and return function as a value from other function. You can create function by using def keyword. You must mention return type of parameters while defining function and return type of a function is optional.


1 Answers

To answer your questions in order:

  • f2 and f5() return Unit because scala takes any def without an "=" to be a function that returns Unit, regardless of what the last item in a block is. This is a good thing, since otherwise it would not be rather verbose to define a function that does not return anything.
  • println(f5()) is valid, even though it returns Unit because in scala Unit is a valid object, though admittedly not one you can instantiate. Unit.toString() is a valid, if not generally useful, statement, for example.
  • Not all the versions that print out 10 are the same. Most importantly, f7,f8, and f9 are actually functions that return functions that return 10, rather than returning 10 directly. When you declare def f8 = () => {10}, you are declaring a function f8 that takes no arguments and returns a function that takes no arguments and returns a single integer. When you invoke println(f8) then f8 dilligently returns that function to you. When you call println(f8()) it returns the function, then immediately invokes it.
  • The functions f1,f3,f4, and f6 are all essentially equivalent in terms of what they do, they vary only in terms of style.

As "user unknown" indicates, the braces are only important for scoping purposes and do not make any difference in your use case here.

like image 196
nonVirtualThunk Avatar answered Sep 20 '22 08:09

nonVirtualThunk