Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why making a difference between methods and functions in Scala?

Tags:

scala

I have been reading about methods and functions in Scala. Jim's post and Daniel's complement to it do a good job of explaining what the differences between these are. Here is what I took with me:

  • functions are objects, methods are not;
  • as a consequence functions can be passed as argument, but methods can not;
  • methods can be type-parametrised, functions can not;
  • methods are faster.

I also understand the difference between def, val and var.

Now I have actually two questions:

  1. Why can't we parametrise the apply method of a function to parametrise the function? And
  2. Why can't the method be called by the function object to run faster? Or the caller of the function be made calling the original method directly?

Looking forward to your answers and many thanks in advance!

like image 833
Julien Gaugaz Avatar asked Feb 25 '12 02:02

Julien Gaugaz


People also ask

Is there a difference between functions and methods?

A method, like a function, is a set of instructions that perform a task. The difference is that a method is associated with an object, while a function is not.

Should I use method or function?

Here's a simple rule of thumb: if the code acts upon a single instance of an object, use a method. Even better: use a method unless there is a compelling reason to write it as a function.

What is the difference between a function and a method function is a named code?

Functions can be called only by its name, as it is defined independently. But methods can't be called by its name only, we need to invoke the class by a reference of that class in which it is defined, i.e. method is defined within a class and hence they are dependent on that class.

Are methods and functions interchangeable?

Some folks use “function” and “method” interchangeably, but there's a small difference: both of them are reusable chunks of code, but methods belong to classes, structs, and enums, whereas functions do not. Because methods always belong to a data type, they have a concept of self that functions do not.


1 Answers

1 - Parameterizing functions.

It is theoretically possible for a compiler to parameterize the type of a function; one could add that as a feature. It isn't entirely trivial, though, because functions are contravariant in their argument and covariant in their return value:

trait Function1[+T,-R] { ... }

which means that another function that can take more arguments counts as a subclass (since it can process anything that the superclass can process), and if it produces a smaller set of results, that's okay (since it will also obey the superclass construct that way). But how do you encode

def fn[A](a: A) = a

in that framework? The whole point is that the return type is equal to the type passed in, whatever that type has to be. You'd need

Function1[ ThisCanBeAnything, ThisHasToMatch ]

as your function type. "This can be anything" is well-represented by Any if you want a single type, but then you could return anything as the original type is lost. This isn't to say that there is no way to implement it, but it doesn't fit nicely into the existing framework.

2 - Speed of functions.

This is really simple: a function is the apply method on another object. You have to have that object in order to call its method. This will always be slower (or at least no faster) than calling your own method, since you already have yourself.

As a practical matter, JVMs can do a very good job inlining functions these days; there is often no difference in performance as long as you're mostly using your method or function, not creating the function object over and over. If you're deeply nesting very short loops, you may find yourself creating way too many functions; moving them out into vals outside of the nested loops may save time. But don't bother until you've benchmarked and know that there's a bottleneck there; typically the JVM does the right thing.

like image 188
Rex Kerr Avatar answered Sep 23 '22 00:09

Rex Kerr