What are the differences between functions and methods in Scala?

Source: Internet
Author: User

A method can appear as part of an expression (call a function and pass a parameter), but the method (with the parameter method) cannot be the final expression,

But the function can appear as the final expression:

Scala>//Define a method

Scala> def m (x:int) = 2*x

M: (x:int) Int

Scala>//define a function

Scala> val f = (x:int) = 2*x

F:int = Int = <function1>

Scala>//method cannot appear as a final expression

Scala> m

<console>:9:error:missing arguments for method m;

Follow this method with ' _ ' If you want to treat it as a partially applied function

M

^

The scala>//function can appear as a final expression

Scala> F

Res9:int = Int = <function1>

The non-parametric method can appear as the final expression, in fact this is a method call, Scala rules the invocation of the parameterless function can omit parentheses

(For the method call we will refer to below)

Scala> def m1 () =1+2

M1: () Int

Scala> M1

Res10:int = 3

The argument list is optional for the method, but is mandatory for the function

Method can have no argument list, and the argument list can be empty. However, the function must have a parameter list (or be empty), see the example below

Scala>//method can have no parameter list

scala> def m2 = 100;

M2:int

Scala>//method can have an empty argument list

scala> def m3 () = 100

M3: () Int

Scala>//function must have parameter list, otherwise error

scala> var f1 = = = 100

<console>:1:error:illegal start of simple expression

var f1 = = = 100

^

The scala>//function can also have an empty argument list

scala> var F2 = () = 100

F2: () = Int = <function0>

So the method can have no parameter list, look down.

The method name implies a method call, and the function name simply represents the function itself

Because the method cannot exist as the final expression, if you write the name of a method and the method has no parameters (no argument list or no parameter)

The expression means: Call the method to get the final expression. Because the function can appear as the final expression, if you write down the function's name, the function

The call does not occur, and the method itself is returned as the final expression, and if you want to force a function to be invoked, you must write () after the function name

Scala>//The method has no argument list

scala> m2

Res11:int = 100

Scala>//The method has an empty argument list

Scala> m3

Res12:int = 100

scala>//Get function itself, no function call occurs

scala> F2

RES13: () = Int = <function0>

scala>//Call function

scala> F2 ()

Res14:int = 100

Why we can provide a method where the function appears

Many of the advanced functions in Scala, such as map (), filter (), are required to provide a function as a parameter. But why do we offer a way to do that?

? Just like this:

scala> val myList = List (3,56,1,4,72)

Mylist:list[int] = List (3, 56, 1, 4, 72)

The scala>//map () parameter is a function

Scala> Mylist.map ((x) = 2*x)

Res15:list[int] = List (6, 112, 2, 8, 144)

Scala>//Try to provide a method for the map () function as a parameter

Scala> def M4 (x:int) = 3*x

M4: (X:int) Int

scala>//Normal execution

Scala> Mylist.map (M4)

Res17:list[int] = List (9, 168, 3, 12, 216)

This is because if we provide a method where the function is expected to occur, the method is automatically converted into a function. This behavior is known as ETA expansion.

In this way, the use of functions will be much simpler. You can verify the behavior by following the code:

Scala>//Where the function is expected to occur, we can use the method

Scala> val f3: (Int) =>int = M4

F3:int = Int = <function1>

scala>//Where functions are not expected to occur, methods are not automatically converted into functions

Scala> val v3 = M4

<console>:8:error:missing arguments for method M4;

Follow this method with ' _ ' If you want to treat it as a partially applied function

Val V3 = M4

^

With this automatic conversion, we can write very concise code, such as the following

Scala>//10.< is interpreted as Obj.method, an orthopedic < method, so the expression is a method that will be interpreted as a function

Scala> Mylist.filter (10.<)

Res18:list[int] = List (56, 72)

Because the operator is interpreted as a method in Scala

Prefix operator: op obj is interpreted as Obj.op

Infix operator: obj1 op obj2 is interpreted as Obj1.op (OBJ2)

Postfix operator: obj op is interpreted as Obj.op

You can write 10<, not 10.<.

Scala> Mylist.filter (10<)

Warning:there were 1 feature warning (s); Re-run with-feature for details

Res19:list[int] = List (56, 72)

How to force a method into a function

You can force a function by adding an underscore to the method name, partially applying a function

scala> val F4 = M4 _

F4:int = Int = <function1>

Scala> F4 (2)

Res20:int = 6

The Bogwang parameter is a method

The Bogwang parameter is essentially a method without a parameter list. That's why you can use name calls without adding ()

Scala>//Using two ' X ' means that two method calls have been made

Scala> def m1 (x: = = Int) =list (x,x)

M1: (x: = = Int) List[int]

scala> import util. Random

Import util. Random

Scala> val r = new Random ()

R:scala.util.random = [email protected]

Scala>//Because the method was called two times, so two values are not equal

Scala> M1 (R.nextint)

Res21:list[int] = List (-1273601135, 2004676878)

If you cache the Bogwang parameter (function) in the method body section, you cache the value (because the X function is called once)

scala>//The function represented by the Bogwang parameter is cached.

Scala> def m1 (x: = = Int) ={val y=x; List (Y,y)}

M1: (x: = = Int) List[int]

Scala> M1 (R.nextint)

Res22:list[int] = List (-1040711922,-1040711922)

It is possible to refer to the method represented by the Bogwang parameter in the Function body section (the method that the Bogwang parameter represents).

Scala> def m1 (x: = = Int) ={val y=x _; List (Y (), Y ())}

M1: (x: = = Int) List[int]

Scala> M1 (R.nextint)

Res23:list[int] = List (-1982925840,-933815401)

What are the differences between functions and methods in Scala?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.