How to compare structs with dynamic arrays

I am trying to compare 2 structs that have dynamic arrays inside of them, here is how the most simple version looks like;

package main

import "core:fmt"

A :: struct {
  d: [dynamic]int,
}

main :: proc() {
  foo := A{make([dynamic]int)}
  bar := A{make([dynamic]int)}

  defer delete(foo.d)
  defer delete(bar.d)

  if (foo == bar) {
    fmt.println("they are the same")
  } else {
    fmt.println("they are not the same")
  }
}

when I tryed to run it I get the following error from the compiler

main.odin(15:7) Error: Cannot compare expression. Type 'A' is not simply comparable, so operator '==' is not defined for it. 
        if (foo == bar) { 
            ^~~~~~~~~^ 

is there a way to make a struct that is not simply comparable into a struct that is, or is there a way to define the ā€˜==’ operator for that struct.

I tried to find more about this but I could not find anything more. (I am very bad at google seaches)

As the error message says, simple compare with == is not possible. You have to write a comparison procedure yourself.

import "core:slice"

A :: struct {
	d: [dynamic]int,
}

equal :: proc (a0, a1: A) -> bool {
	// or just a for loop and compare piecewise...
	return slice.equal(a0.d[:], a1.d[:])
}

This works because int is ā€œsimple compareā€. If it wouldn’t be then you need a version that uses whatever comparator the inner data type needs.

1 Like

thanks, you are right. I am just over thinking things.