Comparing mutable structs with identical field values in Julia

baiango
Wed, 03 Jul 2024 12:52:19 +0000
General Usage

Description

I am working on a project that involves comparing mutable structs with identical field values in Julia. However, I am encountering unexpected results when using the == and === operators, as they return false for mutable structs with identical field values. According to the Julia documentation, the == operator falls back to === for structs.

Here’s a minimal reproducible example:

mutable struct MutRange
	start::Int
	finish::Int
end

println(MutRange(1, 3) === MutRange(1, 3)) # false
println(MutRange(1, 3) == MutRange(1, 3)) # false

Based on the documentation at Mathematics · The Julia Language, the == operator falls back to === for structs. However, in this case, both comparisons return false.

This issue is reported at GitHub issues.

Could someone please provide guidance on how to compare mutable structs correctly with identical field values in Julia? I appreciate any help and expertise you can provide.

Thank you in advance for your assistance!

6 posts - 5 participants