Difference between method and function in Scala -


i read scala functions (part of another tour of scala). in post stated:

methods , functions not same thing

but didn't explain it. trying say?

jim has got pretty covered in his blog post, i'm posting briefing here reference.

first, let's see scala specification tell us. chapter 3 (types) tell function types (3.2.9) , method types (3.3.1). chapter 4 (basic declarations) speaks of value declaration , definitions (4.1), variable declaration , definitions (4.2) , functions declarations , definitions (4.6). chapter 6 (expressions) speaks of anonymous functions (6.23) , method values (6.7). curiously, function values spoken of 1 time on 3.2.9, , no else.

a function type (roughly) type of form (t1, ..., tn) => u, shorthand trait functionn in standard library. anonymous functions , method values have function types, , function types can used part of value, variable , function declarations , definitions. in fact, can part of method type.

a method type non-value type. means there no value - no object, no instance - method type. mentioned above, method value has function type. method type def declaration - def except body.

value declarations , definitions , variable declarations , definitions val , var declarations, including both type , value - can be, respectively, function type , anonymous functions or method values. note that, on jvm, these (method values) implemented java calls "methods".

a function declaration def declaration, including type , body. type part method type, , body expression or block. implemented on jvm java calls "methods".

finally, anonymous function instance of function type (ie, instance of trait functionn), , method value same thing! distinction method value created methods, either postfixing underscore (m _ method value corresponding "function declaration" (def) m), or process called eta-expansion, automatic cast method function.

that specs say, let me put up-front: we not use terminology! leads confusion between so-called "function declaration", part of program (chapter 4 -- basic declarations) , "anonymous function", expression, , "function type", is, type -- trait.

the terminology below, , used experienced scala programmers, makes 1 change terminology of specification: instead of saying function declaration, method. or method declaration. furthermore, note value declarations , variable declarations methods practical purposes.

so, given above change in terminology, here's practical explanation of distinction.

a function object includes 1 of functionx traits, such function0, function1, function2, etc. might including partialfunction well, extends function1.

let's see type signature 1 of these traits:

trait function2[-t1, -t2, +r] extends anyref 

this trait has 1 abstract method (it has few concrete methods well):

def apply(v1: t1, v2: t2): r 

and tell there know it. function has apply method receives n parameters of types t1, t2, ..., tn, , returns of type r. contra-variant on parameters receives, , co-variant on result.

that variance means function1[seq[t], string] subtype of function1[list[t], anyref]. being subtype means can used in place of it. 1 can see if i'm going call f(list(1, 2, 3)) , expect anyref back, either of 2 types above work.

now, similarity of method , function? well, if f function , m method local scope, both can called this:

val o1 = f(list(1, 2, 3)) val o2 = m(list(1, 2, 3)) 

these calls different, because first 1 syntactic sugar. scala expands to:

val o1 = f.apply(list(1, 2, 3)) 

which, of course, method call on object f. functions have other syntactic sugars advantage: function literals (two of them, actually) , (t1, t2) => r type signatures. example:

val f = (l: list[int]) => l mkstring "" val g: (anyval) => string = {   case i: int => "int"   case d: double => "double"   case o => "other" } 

another similarity between method , function former can converted latter:

val f = m _ 

scala expand that, assuming m type (list[int])anyref (scala 2.7):

val f = new anyref function1[list[int], anyref] {   def apply(x$1: list[int]) = this.m(x$1) } 

on scala 2.8, uses abstractfunction1 class reduce class sizes.

notice 1 can't convert other way around -- function method.

methods, however, have 1 big advantage (well, 2 -- can faster): can receive type parameters. instance, while f above can specify type of list receives (list[int] in example), m can parameterize it:

def m[t](l: list[t]): string = l mkstring "" 

i think pretty covers everything, i'll happy complement answers questions may remain.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -