Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mutable vs Ref variables in terms of capture

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.

like image 423
Richard Warburton Avatar asked Oct 19 '10 16:10

Richard Warburton


1 Answers

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).

like image 148
Brian Avatar answered Sep 25 '22 03:09

Brian