If I take your code and do:> let ticketFactory = myRec.GetTicket;;

val ticketFactory : (unit -> int)

> ticketFactory();;
val it : int = 1
> ticketFactory();;
val it : int = 2
> ticketFactory();;
val it : int = 3

Hey, that seems to work...can you figure out the rest?

By on 6/8/2009 4:04 AM ()

Duh! Thanks.

By on 6/8/2009 4:11 AM ()

On second thoughts, I'm still stuck on this. I can see (sort of) how you made the RecordWithClosure.GetTicket work, by binding it, as a function, to the value ticketFactory, but I still can't see how my original call of the record member myRec.GetTicket() is different from the first GetTicket();;

Still, I am convinced :) I even tried it in OCaml and got the same result, in all three cases - my first two, plus kurt's. (although the OCaml syntax for the record is different)

So, if anyone would like to explain the difference, I'd be grateful. Otherwise, I'll keep looking into it.

By on 6/11/2009 5:54 PM ()

Beware that

member this.Foo = ...

is a _property_, which is re-evaluated each time you touch it, whereas

let Foo = ...

is a value, which is evaluated once. Put a printf as the first line of each, and watch the difference when you multiply refer to 'Foo'.

By on 6/11/2009 6:04 PM ()

Thanks Brian - you've hit the nail on the head.

I was getting confused between a function value (my original GetTicket, and kurt's ticketFactory) and an object value (my myRec). A function value, and its closure, is persistent, and an object value is also persistent, but an object's property is re-evaluted each time.

Revisting my sample...

"> myRec.GetTicket();;
val it : int = 1 // <--- myRec.GetTicket() has re-initialized its closure - NOT

In fact, this is what happened...

> myRec.GetTicket();;
val it : int = 1 // <-- myRec.GetTicket has returned a function,
// with a closure, and we applied it to ()
> myRec.GetTicket();;
val it : int = 1 // <-- myRec.GetTicket has returned another function,
// with another closure, and we applied it to ()

This is distinct from...

> ticketFactory();;
val it : int = 1 // Apply a function in its closure
> ticketFactory();;
val it : int = 2 // Apply the same function, in the same closure

Thankyou, heaps, Brian and kurt

By on 6/11/2009 9:34 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper