Duncan McGregor
Duncan McGregor

Reputation: 18157

What is a non-method-local var in 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?

Upvotes: 5

Views: 163

Answers (2)

tgr
tgr

Reputation: 3608

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.

Upvotes: 3

aioobe
aioobe

Reputation: 420951

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.

Upvotes: 7

Related Questions