Julia: immutable compound types

I am still completely unfamiliar with julia and very annoyed with the following behavior:

immutable X x::ASCIIString end "Foo" == "Foo" true X("Foo") == X("Foo") false 

but instead of Int instead of ASCIIString

 immutable Y y::Int end 3 == 3 true Y(3) == Y(3) true 

I expected X("Foo") == X("Foo") be true. Can anyone explain why this is not so?

Thanks.

+7
hash julia-lang
source share
1 answer

Julia has two types of equality comparison:

  • If you want to verify that x and y are identical, in the sense that no program can tell them apart. Then the right choice is to use the is(x,y) function, and the corresponding operator, this type of comparison is the === operator. The tricky part is that two mutable objects are equal if their memory addresses are identical, but when comparing two immutable objects, is returns true if the contents are the same at the bit level.

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

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

  1. Operator
  2. == or its equivalent isequal(x,y) function, which is called general matching and returns true if, firstly, a suitable method for this type of argument exists, and secondly, this method returns true. so what if this method is not specified? then == call the === operator.

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

EDIT:

as mentioned in @Andrew, refer to the Julia documentation, strings have immutable data types, so why "test"!=="true" #=> true ? If you look at the structure of the String data type, for example, xdump("test") #=> ASCIIString data: Array(UInt8,(4,)) UInt8[0x74,0x65,0x73,0x74] , you will find that the rows have composite data types with the important data field. Julia strings are basically a sequence of bytes that is stored in a data field of type String. and isimmutable("test".data) #=> false

+5
source share

All Articles