This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Functional Programming and the fly likes variable bindings and shadowing 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 » Engineering » Functional Programming
Bookmark "variable bindings and shadowing" Watch "variable bindings and shadowing" New topic
Author

variable bindings and shadowing

J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 842
    
  13

I'm guessing there are a few folks on here who started the Coursera course this week. I'm confused by the section on shadowing. Here's the example code (this is SML):


According to the instructor, line 3 is not an assignment statement, but how can it be anything else? It's changing the value of a, but then we're reminded that in ML all variables (poor name...symbols?) are immutable and cannot be changed.

This seems contradictory. It's explained that it doesn't change the value of "a", just creates a new "a" in the local static environment, but that smacks of semantics. The bottom line is, you have a symbol called "a" and at two different points in the code it has two different values.

What am I missing?


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

I just answered exactly that question on the Coursera forum with my understanding of what he means by it, which I'll repeat here.

I think the reason you can't tell the difference in that example is that they're all top-level bindings, which means they never go out of scope. But if you change the construct so that the scope of the second binding is limited you can see the difference (and it probably works as you'd expect it to in non-functional languages as well):
That final line will bind b to 10, showing that the original value of a hasn't been overwritten. More generally, without assignment there's nothing you can do in the scope-limited block that will change the value of a once you've exited that scope.

J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 842
    
  13

I just saw your post on the Coursera forum while searching for answers. Funny.

Using scope does make it somewhat clearer, but I'm still surprised that the compiler allows this at all. I would think that it's similar to trying to reassign a final variable in Java. You shouldn't be able to do that. The fact that it's possible will only lead to mass confusion. To say on the one hand that all variables are immutable and then on the other hand write code that assigns different values to the same variable names is just way too confusing and contradictory.

Somehow I still feel like I'm missing some key concept here. I'll keep searching and reading...
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Well, Java has shadowing as well. You can have a method parameter or a local variable clash with a class-level variable. Or a subclass can declare a member variable that shadows a member variable in the super-class. This is just the same as that, only it's more general.

I suppose that generally speaking it would be poor style to reuse variable names like this. Especially if they have different meanings. But when you're using nested functions it can be quite useful to be able to re-use method parameter names sometimes (when the meaning is clear).

Sean Corfield
Ranch Hand

Joined: Feb 09, 2011
Posts: 252
    
    5

There's an example in one of the later lectures showing how the earlier bindings have <hidden value>s when the REPL prints the output of the file. That's intended to show that each binding creates a "new environment", both static and dynamic, rather than updating an earlier 'variable'. It's something you'll quickly get used to with Functional Programming and if you use Clojure, you'll see the same thing in that:

The second def doesn't really update a, it changes the binding for the symbol #'user/a to refer to the value 13. However, in Clojure you can actually get at the underlying Var object itself:

This looks a lot more like assignment - because there is a level of indirection here - but it really gets in the way of understanding the generally immutable nature of functional programming to think of it that way.

And of course Clojure isn't quite as "pure" as ML in terms of immutability. Bindings can be altered, per-thread or globally, and there are specific, limited mutable data types (atom, ref) which are controlled via STM to provide atomic / coordinated transactional updates.

Hope that helps?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: variable bindings and shadowing
 
Similar Threads
change event is firing twice
Does the jQuery val() function not work in IE7 & IE8
Passing Object reference
Problem using StringTokenizer
Loop problems