Scala Def method declaration: Colon vs equals

I am in the early stages of learning Scala, and I noticed different ways of declaring methods.

I found that not using the sign equals the method with the void method (returns the value of Unit instead of the value), and using the equal sign returns the actual value, therefore

 def product(x: Int, y: Int) { x*y } 

will return () (Unit), but

 def product(x: Int, y: Int) = { x*y } 

will return the product of two arguments ( x*y )

I noticed a third way to declare methods - with a colon. Here is an example

 def isEqual(x: Any): Boolean 

How is this different from = notation? And in what situations is it best to use this method?

+5
source share
6 answers

When you use a colon (and use an equal), you explicitly determine the type of method returned.

 // method return Boolean value def m(a : Int) : Boolean = a > 0 

If you do not use a colon and use an equal, you allow the scala compiler to output the return type itself.

 // method has return type of last expression (Boolean) def m(a : Int) = a > 0 

If you do not use a colon or equal, your method returns the type of the Unit class.

 // method has Unit return type def m(a : Int){ a > 0 } 
+10
source

if you do not need the type of return you used

 scala> def product(x: Int, y: Int) { //in lack of = it explicit uses Unit | x*y | } product: (x: Int, y: Int)Unit //equivalent to below scala> def product(x: Int, y: Int):Unit={ | x*y | } product: (x: Int, y: Int)Unit 

and when you write

 scala> def product(x: Int, y: Int) = { //here it explicit detect return type | x*y | } product: (x: Int, y: Int)Int //equivalent to below scala> def product(x: Int, y: Int):Int = { | return x*y | } product: (x: Int, y: Int)Int 
+1
source

Others explained the differences between the various ads perfectly:

 def foo: Boolean = true // explicitly declare the return type 

and

 def foo = true // let the compiler to implicitly infer Boolean 

Having said that, I must warn you against

 def foo { } 

This is called procedural syntax, and you should never use it because it has already been deprecated (since October 29, 2013), although you will get an obsolescence warning only with the -Xfuture flag.

Whenever you have to declare a method that returns Unit (which should be avoided as much as possible, as this means you rely on side effects), use the following syntax

 def foo: Unit = { } 

Also, as a personal tip, explicitly annotating the return types makes your code more readable and helps you quickly spot errors. You know that a function should return, so explicit type annotation allows the compiler to verify that the implementation returns the corresponding value, right at the declaration position (if you use this function, you will end up catching the error, but maybe far from where the error is really is).

Also, when declaring an abstract element, you'd better comment on the types as well

 trait { def foo } 

is legal, but the type foo automatically detected as Unit , which you almost never need.

Instead do

 trait { def foo: Boolean } 
+1
source

def foo(arg) { sideEffects() } equivalent to def foo(arg) = { sideEffects(); Unit } def foo(arg) = { sideEffects(); Unit } . This is a bad style because it hides your intention. (It’s easy to miss the missing = ). I recommend you not to use it.

def foo(arg) = { expression } is a method that implicitly resolves an expression type and is a good style.

def foo(arg): Type is an abstract method definition with an explicit type. This is not equivalent to the other two examples, since no implementation is provided.

0
source

1) procedure syntax deprecated from scala -2.11, avoid this.

 def product(x: Int, y: Int) { x*y } // It always returns Unit. // creates confusion for most beginners. deprecated for good. 

2) type inference syntax, mainly used with small / private methods.

 def product(x: Int, y: Int) = { x*y } 

This is help from the compiler, but sometimes the output of the compiler may be different from what you think.

3) Syntax of type annotation .

 def product(x: Int, y: Int): Int = { x*y } 

For public APIs, it is good to specify the type of the return value explicitly.

In some cases, you must specify the type of the return value. For instance:

  • in defining recursive methods.

  • when the compiler output is more specific, but you need a general return type (Map instead of HashMap).

  • To override compiler output. def getId(): Long = 1

  • If return used anywhere in the body of the method.

0
source

In Scala, a function is defined as

 def product(x: Int, y: Int): Int = { x*y } 

but there are a few shortcuts you can use

  • Since Scala has a good output type, you can omit the type (unless the function is recursive):

    def product(x: Int, y: Int) = { x*y }

  • All this expression. therefore, the function should return a value. The last line of the code block is the return value. therefore, if you use curved braces {}, the last line will be returned. if the function has only one line, there is no need for curved brackets.

    def product(x: Int, y: Int) = x*y

  • Since everything should return the value "No value" actually Unit . Therefore, if you do not want to return a meaningful value (which means that you are doing some side effects), use the value of Unit as the return value:

    def product(x: Int, y: Int): Unit = x*y

  • Using functions without an equal sign is another way to return Unit (this is also called "procedure syntax":

    def product(x: Int, y: Int) { x*y }

    • Please note that the procedure syntax is deprecated and should be avoided!
0
source

Source: https://habr.com/ru/post/1205929/


All Articles