Functions

Download scala eBook

Remarks

Scala has first-class functions.

Difference between functions and methods:

A function is not a method in Scala: functions are a value, and may be assigned as such. Methods (created using def), on the other hand, must belong to a class, trait or object.

  • Functions are compiled to a class extending a trait (such as Function1) at compile-time, and are instantiated to a value at runtime. Methods, on the other hand, are members of their class, trait or object, and do not exist outside of that.
  • A method may be converted to a function, but a function cannot be converted to a method.
  • Methods can have type parameterization, whereas functions do not.
  • Methods can have parameter default values, whereas functions can not.

Related Examples

Stats

239 Contributors: 20
Tuesday, October 25, 2016
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook