Julia:不可变的复合类型 [英] Julia: Immutable composite types

查看:173
本文介绍了Julia:不可变的复合类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 不可变X 
x :: ASCIIString
结束

Foo==Foo
true
X(Foo)== X(Foo)
false

但使用Int而不是ASCIIString

 immutable Y 
y :: Int
end

3 == 3
true
Y(3)== Y(3)
true

我曾预计 X( Foo)== X(Foo)为真。任何人都可以澄清为什么它不是?



谢谢。

解决方案

Julia有两种类型的相等比较:


  1. 如果您想检查 x和y是否相同,没有程序可以区分它们。然后正确的选择是使用 is(x,y)函数,并且执行此类比较的等效运算符是 === 运算符。棘手的部分是,如果两个可变对象的内存地址相同,则两个可变对象相等,但当比较两个不可变对象,如果内容在位级别相同,则返回true。

2 === 2#=> true,因为数字是不可变的



Foo===Foo#=> false


  1. == 运算符或它相当于 isequal(x,y)函数称为泛型比较,并返回true,如果首先为此类型参数其次,该方法返回true。那么如果该方法未列出,该怎么办?然后 == 调用 === 运算符。

现在对于上面的例子,你有一个不可变类型,它没有 == 操作符,所以你真的调用 === 运算符,它检查两个对象在位级别的内容是否相同,并且不是因为它们指向不同的字符串对象,而是Foo!== Foo





编辑:



as @Andrew提到,参考Julia文档,字符串是不可变的数据类型,所以为什么test!= =true#=>真正?如果您向下查看String数据类型的结构,例如 xdump(test)#=> ASCIIString data:Array(UInt8,(4,))UInt8 [0x74,0x65,0x73,0x74] ,你发现字符串是一个重要的 data 字段。 Julia字符串主要是存储在String类型的 data 字段中的一系列字节。和 isimmutable(test.data)#=>假


I am still totally new to julia and very irritated by the following behaviour:

immutable X
  x::ASCIIString
end

"Foo" == "Foo"
true
X("Foo") == X("Foo")
false

but with Int instead of ASCIIString

immutable Y
  y::Int
end

3 == 3
true
Y(3) == Y(3)
true

I had expected X("Foo") == X("Foo") to be true. Can anyone clarify why it is not?

Thank you.

解决方案

Julia have two types of equality comparison:

  1. If you want to check that x and y are identical, in the sense that no program could distinguish them. Then the right choice is to use is(x,y) function, and the equivalent operator to do this type of comparison is === operator. The tricky part is that two mutable objects is equal if their memory addresses are identical, but when you compare two immutable objects is returns true if contents are the same at the bit level.

2 === 2 #=> true, because numbers are immutable

"Foo" === "Foo" #=> false

  1. == operator or it's equivalent isequal(x,y) function that is called generic comparison and returns true if, firstly suitable method for this type of argument exists, and secondly that method returns true. so what if that method isn't listed? then == call === operator.

Now for the above case, you have an immutable type that do not have == operator, so you really call === operator, and it checks if two objects are identical in contents at bit level, and they are not because they refer to different string objects and "Foo" !== "Foo"

EDIT:

as @Andrew mentioned, refer to Julia documentation, Strings are of immutable data types, so why "test"!=="true" #=> true? If you look down into structure of a String data type e.g by xdump("test") #=> ASCIIString data: Array(UInt8,(4,)) UInt8[0x74,0x65,0x73,0x74], you find that Strings are of composite data types with an important data field. Julia Strings are mainly a sequence of bytes that stores in data field of String type. and isimmutable("test".data) #=> false

这篇关于Julia:不可变的复合类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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