问题描述
初始注释::我正在朱莉娅(Julia)工作,但是这个问题可能适用于多种语言.
Initial note: I'm working in Julia, but this question probably applies to many languages.
设置:我的复合类型如下:
Setup: I have a composite type as follows:
type MyType
x::Vector{String}
end
我写了一些对MyType
起作用的方法.例如,我编写了一种方法,该方法允许我在x
中插入一个新元素,例如function insert!(d::MyType, itemToInsert::String)
.
I write some methods to act on MyType
. For example, I write a method that allows me to insert a new element in x
, e.g. function insert!(d::MyType, itemToInsert::String)
.
问题:MyType
是可变的还是不可变的?
Question: Should MyType
be mutable or immutable?
我的理解:我已阅读 Julia文档以及与此相关的关于Stackoverflow的更常见(且被强烈推崇的)问题(例如或此处),但我仍然不知道从实际的角度来看,可变性/不可变的的含义并没有真正弄清楚(特别是对于包含可变类型的可变数组的不可变复合类型的情况!)
My understanding: I've read the Julia docs on this, as well as more general (and highly upvoted) questions on Stackoverflow (e.g. here or here), but I still don't really have a good handle on what it means to be mutable/immutable from a practical perspective (especially for the case of an immutable composite type, containing a mutable array of immutable types!)
不过,这是我的尝试:如果MyType
是不可变的,则意味着字段x
必须始终指向同一对象.该对象本身(字符串的向量)是可变的,因此对于我来说,在其中插入新元素是完全可以的.我不允许做的是尝试更改MyType
,以便字段x
指向完全不同的对象.例如,可以执行以下操作的方法:
Nonetheless, here is my attempt: If MyType
is immutable, then it means that the field x
must always point to the same object. That object itself (a vector of Strings) is mutable, so it is perfectly okay for me to insert new elements into it. What I am not allowed to do is try and alter MyType
so that the field x
points to an entirely different object. For example, methods that do the following are okay:
MyType.x[1] = "NewValue"
push!(MyType.x, "NewElementToAdd")
但是执行以下操作的方法不可行:
But methods that do the following are not okay:
MyType.x = ["a", "different", "string", "array"]
这是对的吗?另外,是否将不可变类型字段值锁定为的对象是在构造函数中创建的对象?
Is this right? Also, is the idea that the object that an immutable types field values are locked to are those that are created within the constructor?
最后一点::对于这似乎在SO上重复了其他问题,我深表歉意.如前所述,我已经浏览了它们,但无法获得我所追求的理解.
Final Point: I apologise if this appears to duplicate other questions on SO. As stated, I have looked through them and wasn't able to get the understanding that I was after.
推荐答案
所以(至少对我来说)这是我需要考虑的事情:
So here is something mind bending to consider (at least to me):
julia> immutable Foo
data::Vector{Float64}
end
julia> x = Foo([1.0, 2.0, 4.0])
Foo([1.0,2.0,4.0])
julia> append!(x.data, x.data); pointer(x.data)
Ptr{Float64} @0x00007ffbc3332018
julia> append!(x.data, x.data); pointer(x.data)
Ptr{Float64} @0x00007ffbc296ac28
julia> append!(x.data, x.data); pointer(x.data)
Ptr{Float64} @0x00007ffbc34809d8
因此,data
地址实际上随着向量的增长而改变,需要重新分配!但是-正如您指出的那样,您不能自己更改数据.
So the data
address is actually changing as the vector grows and needs to be reallocated! But - you can't change data yourself, as you point out.
我不确定是否确实有100%正确的答案.在某些对性能有严格要求的情况下,我主要将immutable
用于简单类型,例如文档中的Complex
示例,而出于防御性编程"的原因,例如该代码无需写入此类型的字段,因此这样做是错误的.每当类型是数字的扩展名(例如. Complex
,RGBColor
,我用它们代替了元组,作为一种命名的元组(元组现在无论如何在Julia中都表现不佳,其中不可变类型的性能很好).
I'm not sure there is a 100% right answer is really. I primarily use immutable
for simple types like the Complex
example in the docs in some performance critical situations, and I do it for "defensive programming" reasons, e.g. the code has no need to write to the fields of this type so I make it an error to do so. They are a good choice IMO whenever the type is a sort of an extension of a number, e.g. Complex
, RGBColor
, and I use them in place of tuples, as a kind of named tuple (tuples don't seem to perform well with Julia right now anyway, wheres immutable types perform excellently).
这篇关于在Julia中了解具有可变类型字段的不可变复合类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!