Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a non-method-local var in Scala?

Tags:

scala

In the comments to Scala Sink or Swim Josh Sureth calls out non-method-local vars as a cause of problems in Scala code.

That page is the only Google hit for the phrase non-method-local vars, so what does it mean, and what is the problem?

like image 618
Duncan McGregor Avatar asked May 05 '12 12:05

Duncan McGregor


2 Answers

A method local variable is a local variable declared in the scope of a method.

Consequently a non-method-local variable ought to be a variable with a wider scope, such as class scope.

Can't tell for sure why one would say that they are problematic. Perhaps it is simply due to the fact that they introduce a mutable state in objects.

like image 72
aioobe Avatar answered Sep 29 '22 08:09

aioobe


The problem with non-method-local vars is that they introduce mutable states to a class/object. This is something you should avoid whereever you can, because scala is a functional language as well. (In pure functional languages like Haskell variables are forbidden.) Those variables start producing even more problems when you start working parrallel.

like image 26
tgr Avatar answered Sep 29 '22 07:09

tgr