On 05/03/2014, at 5:40 AM, srean wrote:

> 
> My personal preference is to capture variables as values, Python doesnt do 
> this and I can still use Python because I understand what is happening inside 
> Python and I know what I will get.

No one does that, you'd just not understanding the mechanisms
correctly. In Ocaml:

let f x y = x + y;;
let cls = ref [];;
for i=0 to 4 do
  cls := (fun () -> print_endline (string_of_int i)) :: !cls
done;;
List.iter
  (fun f -> f())
(!cls);;

appears to capture the value of i. But you have to understand
a loop is an abbreviation for tail recursion, and i isn't a variable.

In the functional part of Ocaml, every let binding is a pointer to a new
value on the heap (actually its a box, everything is boxed except integers).

So Ocaml doesn't use scopes, there is no pointer to the parent scope.
Instead, Ocaml does uses pointers to EVERY value. Separate pointers.
So it copies the boxes. It uses this mechanism because it has a copying
collector and this method gets rid of more garbage.

In fact, if you use a mutable value instead of an invariant one like an integer,
say a Hashtable or a reference .. you will have the same issue.

It doesn't have anything to do with your preference for how closures
work. It's got to do with scopes and how VARIABLES work.

The simple fact is Felix closures copy parameters as you'd expect,
but they don't copy non-parameter variables, also as you'd expect.
If they did copy non-parameter variable this code would never work:

        var x = 1;
        fun f () => x;
        var g = f;
        ++x;
        println$ g ();

you'd expect this to print 2. It is, after all, clearly a function that gives
the current value of a variable.


--
john skaller
skal...@users.sourceforge.net
http://felix-lang.org




------------------------------------------------------------------------------
Subversion Kills Productivity. Get off Subversion & Make the Move to Perforce.
With Perforce, you get hassle-free workflows. Merge that actually works. 
Faster operations. Version large binaries.  Built-in WAN optimization and the
freedom to use Git, Perforce or both. Make the move to Perforce.
http://pubads.g.doubleclick.net/gampad/clk?id=122218951&iu=/4140/ostg.clktrk
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to