可变结构比可变结构有什么好处? [英] What are the benefits of an immutable struct over a mutable one?

查看:76
本文介绍了可变结构比可变结构有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经知道不变性比可变性的好处在于能够推理代码并引入较少的错误,尤其是在多线程代码中.但是,在创建结构时,与在可变结构上创建完全不变的结构相比,我看不出任何好处.

I already know the benefit of immutability over mutability in being able to reason about code and introducing less bugs, especially in multithreaded code. In creating structs, though, I cannot see any benefit over creating a completely immutable struct over a mutable one.

让我们以保留一些得分的结构为例:

Let's have as an example of a struct that keeps some score:

struct ScoreKeeper {
    var score: Int
}

在这种结构中,我可以更改现有struct变量上的score值

In this structure I can change the value of score on an existing struct variable

var scoreKeeper = ScoreKeeper(score: 0)
scoreKeeper.score += 5
println(scoreKeeper.score)
// prints 5

不变版本看起来像这样:

The immutable version would look like this:

struct ScoreKeeper {
    let score: Int

    func incrementScoreBy(points: Int) -> ScoreKeeper {
        return ScoreKeeper(score: self.score + points)
    }
}

及其用法:

let scoreKeeper = ScoreKeeper(score: 0)
let newScoreKeeper = scoreKeeper.incrementScoreBy(5)
println(newScoreKeeper.score)
// prints 5

我看不到第二种方法比第一种方法的好处,因为结构是值类型.如果我传递一个结构,它总是被复制.因此,对我来说,结构是否具有可变属性并不重要,因为代码的其他部分仍将在单独的副本上工作,从而消除了可变性的问题.

What I don't see is the benefit of the second approach over the first, since structs are value types. If I pass a struct around, it always gets copied. So it does not seem to matter to me if the structure has a mutable property, since other parts of the code would be working on a separate copy anyway, thus removing the problems of mutability.

不过,我看到有些人在使用第二个示例,该示例需要更多代码,但并没有明显的好处.我看不到有什么好处吗?

I have seen some people using the second example, though, which requires more code for no apparent benefit. Is there some benefit I'm not seeing?

推荐答案

不同的方法将有助于对代码进行各种更改.不变结构与不变类对象非常相似,但是可变结构和可变类对象却有很大不同.因此,如果出于某种原因有必要改为使用类对象,则使用不可变结构的代码通常可以很容易地进行修改.

Different approaches will facilitate different kinds of changes to the code. An immutable structure is very similar to an immutable class object, but a mutable structure and a mutable class object are very different. Thus, code which uses an immutable structure can often be readily adapted if for some reason it becomes necessary to use a class object instead.

另一方面,使用不可变对象通常会使代码将变量替换为修改后的版本变得更加脆弱,以防万一将其他属性添加到有问题的类型中.例如,如果PhoneNumber类型包括AreaCode,LocalExchange和LocalNumber的方法以及采用这些参数的构造函数,然后为Extension添加可选"第四个属性,则应该更改某些区域代码的代码通过将新的区号LocalExchange和LocalNumber传递给三参数构造函数,可以删除每个电话号码的Extension属性,而可以直接写入AreaCode的代码则不会出现此问题.

On the flip side, use of an immutable object will often make the code to replace a variable with a modified version more brittle in case additional properties are added to the type in question. For example, if a PhoneNumber type includes methods for AreaCode, LocalExchange, and LocalNumber and a constructor that takes those parameters, and then adds an "optional" fourth property for Extension, then code which is supposed to change the area codes of certain phone numbers by passing the new area code, LocalExchange, and LocalNumber, to the three-argument constructor will erase the Extension property of every phone number, while code which could write to AreaCode directly wouldn't have had that problem.

这篇关于可变结构比可变结构有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆