This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA 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: 9353
    
    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!
 
Don't get me started about those stupid light bulbs.
 
subject: Mutating state inside a function