wood burning stoves 2.0*
The moose likes Scala and the fly likes Mutating state inside a function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Languages » Scala
Bookmark "Mutating state inside a function" Watch "Mutating state inside a function" New topic
Author

Mutating state inside a function

Gabriel Beres
Ranch Hand

Joined: Sep 09, 2006
Posts: 61
Hey,

I understand why mutating instance variables is a bad thing.
But what about mutating state inside a function? To my knowledge, variables inside the function are allocated in the stack and not in the heap, therefore they are not objects of any concurrency issue.

Scala and other functional languages say you should not mutate any state. Why it is so?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Gabriel Beres wrote:Hey,

I understand why mutating instance variables is a bad thing.
But what about mutating state inside a function? To my knowledge, variables inside the function are allocated in the stack and not in the heap, therefore they are not objects of any concurrency issue.

Scala and other functional languages say you should not mutate any state. Why it is so?


That is a good question. Have a look here about Referential Transparency to better answer your question.

http://en.wikipedia.org/wiki/Referential_transparency_%28computer_science%29


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mutating state inside a function
 
Similar Threads
static variable inside static function
Declaring static variable.
protected
Reentrant vs Thread Safety
How to know a class is Immutable or Not