For future reference, I'd really appreciate you not referring to my writing as "quite erroneous" when the claims made are in fact correct.
-- John On Sep 11, 2014, at 2:38 PM, Mohammed El-Beltagy <[email protected]> wrote: > In a recent blog by John Myles White > http://www.johnmyleswhite.com/notebook/2014/09/06/values-vs-bindings-the-map-is-not-the-territory/ > it was suggested that Julia make strong distinction between values and > binding. For example > a = [1, 2, 3] > function foo!(a) > a[1] = 10 > return > end > a > > Would give an output > 3-element Array{Int64,1}: > 10 > 2 > 3 > But subsequently running > function bar!(a) > a = [1, 2] > return > end > a > would give an output of > 3-element Array{Int64,1}: > 10 > 2 > 3 > The reason why the variable "a" was not changed in the case of "boo!", but > was changed in the case of "foo!" has to do with the fact values are the same > their binding. > However to illustrate that point, the examples given in the blog are quite > erroneous. For example > [1,2,3]==[1,2,3] > would yield an out of "true", whereas the blog post indicated that is should > be "false". This the because the "==" operator unpacks the values in the > binding and does and element wise comparison. > A better was illustrate that problem is make use of julia's "object_id" > function. For example > a=[1,2,3] > @printf("Object ID in outerscope 0x%x\n", object_id(a)) > function bar!(a) > @printf("Object ID in \!bar and before assignment 0x%x\n", object_id(a)) > a = [1, 2] > @printf("Object ID in \!bar after assignment 0x%x\n", object_id(a)) > return > end > bar!(a) > @printf("Object ID in outerscope after \!bar is 0x%x\n", object_id(a)) > a > The output will be > Object ID in outerscope 0xa19caeb70115d4f0 > Object ID in !bar and before assignment 0xa19caeb70115d4f0 > Object ID in !bar after assignment 0x782193112dfbbbd5 > Object ID in outerscope after !bar is 0xa19caeb70115d4f0 > Out[1]: > 3-element Array{Int64,1}: > 1 > 2 > 3 > This would mean the when calling a function in Julia the following is true: > The binding of the argument to the function can never change, though the > values of that binding might. > The binding can be overridden locally in the scope of the function, but that > is only valid in inside the function's scope > A modification to the above code that does not attempt to override the > original binding, shown below would not change the object id. > a=[1,2,3] > @printf("Object ID in outerscope 0x%x\n", object_id(a)) > function bar!(a) > @printf("Object ID in \!bar and before assignment 0x%x\n", object_id(a)) > empty!(a) > push!(a,[1, 2]...) > @printf("Object ID in \!bar after assignment 0x%x\n", object_id(a)) > return > end > bar!(a) > @printf("Object ID in outerscope after \!bar is 0x%x\n", object_id(a)) > a > > Is this how julia handles values and bindings, or it there more to this > picture? >
