开发者

Mutable vs Ref variables in terms of capture

开发者 https://www.devze.com 2023-01-20 16:25 出处:网络
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 add

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 f开发者_如何学Gorom 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消