可变或不可变闭包

发布于 2024-07-16 14:14:11 字数 475 浏览 12 评论 0原文

在命令式、面向对象的语言中,使用可变或不可变的闭包更有意义吗?

例如:

int i=5;
function() f={print(i);};
f();
i=6;
f();

如果闭包是可变的,这将打印:

5
6

如果它是不可变的,它将打印:

5
5

我意识到即使使用不可变的闭包,你仍然可以这样做:

class I {int i;}
I i=new I();
i.i=5;
function() f={
    I j=i;
    print(j.i);
};
f();
i.i=6;
f();

那么,拥有可变或不可变的闭包会更好吗?或者两者都有选择? 不可变的闭包似乎更容易实现,所以在这一点上,我想我会这样做,除非有充分的理由不这样做。

In an imperative, object orients language, would make more sense to have mutable or immutable closures?

For example:

int i=5;
function() f={print(i);};
f();
i=6;
f();

If the closure is mutable, this would print:

5
6

If it is immutable, it would print:

5
5

I realize that even with immutable closures, you could still do this:

class I {int i;}
I i=new I();
i.i=5;
function() f={
    I j=i;
    print(j.i);
};
f();
i.i=6;
f();

So, would it be better to have mutable, or immutable closures, or have the option for both? Immutable closures seem easier to implement, so at this point, I think I'll go with that, unless there is a good reason not to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

压抑⊿情绪 2024-07-23 14:14:11

命令式语言通常是围绕状态概念构建的。 因此,语言特征(包括闭包)反映这一点更有意义。 是的,这种行为有时可能会令人困惑,但它是问题的一部分,也是在应用程序中拥有状态的优势。

我认为这个论点的最好证明是看看一些最近支持闭包的语言。 C# 和 VB.Net 这两种命令式面向对象语言都选择了可变闭包。 而函数式语言 F# 具有不可变闭包(主要源自 F# 默认情况下不可变的思想)。

另外,在命令式语言中拥有不可变闭包实际上意味着什么? 大多数人认为这使变量相当于 C# 的只读变量。 当然值类型会受到保护,不被修改,但是可变引用类型又如何呢? 您无法更改变量指向的位置,但可以调用变异函数并获得类似的效果。 例如。

class Student {
  public string Name { get; set; }
}

void Example() {
  var student = new Student() { Name = "foo" };
  Action() del = () => 
    { student.Name = "bar"; };
  del();
}

这可以通过不可变的闭包来实现,因为我实际上并不修改变量指向的位置。 然而我显然仍在进行变异操作。

Imperative languages are typically built around the concept of state. Hence it makes more sense for language features to reflect that, including closures. Yes, this behavior can be confusing at times but it's part of the problem and advantage to having state in your application.

I think the best proof of this argument is to look at some of the more recent languages that have closure support. Both C# and VB.Net, imperative OO languages, chose to have mutable closures. While F#, a functional language, has immutable closures (mainly deriving from the idea that F# is immutable by default).

Also what would it actually mean to have an immutable closure in an imperative language? Most people think of this as making the variables equivalent to C#'s readonly. Sure value types would be protected from modification, but what about mutable reference types. You wouldn't be able to change where the variable pointed to, but you could call a mutating function and get a similar effect. For example.

class Student {
  public string Name { get; set; }
}

void Example() {
  var student = new Student() { Name = "foo" };
  Action() del = () => 
    { student.Name = "bar"; };
  del();
}

This could be implemented with an immutable closure as I don't actually modify where the variables point to. However I am clearly still doing a mutating operation.

阳光①夏 2024-07-23 14:14:11

语言是否应该具有按值捕获或按引用捕获的 lambda? 自行决定,但请参阅“关于 lambda、捕获和可变性”以获得更多评论。

Ought languages have lambdas that capture by value or capture by reference? Decide for yourself, but see "On lambdas, capture, and mutability" for more commentary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文