#575 Bug in List.equals?

KevinKelley Tue 5 May 2009

sys::List fandoc says:

Two Lists are equal if they have the same number of items and all the items at each index return true for equals().

but this code:

d1 := Obj[1,2,3,4,5,6,7,8,9]
d2 := Int[1,2,3,4,5,6,7,8,9]
Int[] d3 := (Int[])d1

echo("d1: $d1 is ${d1.type}")
echo("d2: $d2 is ${d2.type}")
echo("d3: $d3 is ${d3.type}")
echo("d1 == d2: ${d1==d2}")
echo("d1 == d3: ${d1==d3}")
echo("d2 == d3: ${d2==d3}")

gives this output:

d1: [1, 2, 3, 4, 5, 6, 7, 8, 9] is sys::Obj[]
d2: [1, 2, 3, 4, 5, 6, 7, 8, 9] is sys::Int[]
d3: [1, 2, 3, 4, 5, 6, 7, 8, 9] is sys::Obj[]
d1 == d2: false
d1 == d3: true
d2 == d3: false

Ignoring the bit about d3 showing as Obj[] in spite of the typing and the cast; I'm thinking that d1 == d2 should be true, not false. Same size, contains equal Int values.

brian Tue 5 May 2009

The docs are incorrect - the types of the two lists must match also:

Str[,] != Str?[,]

That can be a gotcha especially in testing for verifyEq, but I definitely think it is the correct behavior (especially for testing).

I will fix the docs.

KevinKelley Tue 5 May 2009

the types of the two lists must match also

Not sure I agree with that; doesn't == usually mean equivalent, as opposed to identical? Not sure I'm expressing what I mean. But for example it's usually true that 1 == 1.0, even though the types are different.

Actually that 1==1f isn't legal in Fan I guess. But at least there I get a compile error, instead of a silent failure.

I don't know; it just seemed like it was so useful and sensible, the way it was documented: Lists are equal if their contents evaluate as equal. I could wish it really worked that way.

Login or Signup to reply.