views:

81

answers:

1

My superficial understanding of variables in f# suggests that declaring a variable to be 'mutable' and using a 'ref' variable essentially both do the same thing. They are both different ways to address the same underlying issue - a limited and structured allowance of mutability in a functional language without having to resort to the IO Monad. That there is a technical different has been 'abstracted' by my understanding.

  1. If this is the case why can't closures capture mutable variables, but they can capture ref instances?
  2. More generally, what is the technical different between the two forms that allows this difference?
  3. What is the purpose from a language design point of view of introducing two mutability shortcuts rather than just one?

I'm sorry if this is a multi-parter, but they all seem related.

+5  A: 

See

http://lorgonblog.wordpress.com/2008/11/12/on-lambdas-capture-and-mutability/

especially the "language design commentary" section (I'd quote it here, but it doesn't stand alone well, you need the whole blog entry for context).

Brian