Skip to content

Type-safe equality operators #40717

@jtrakk

Description

@jtrakk

I often make mistakes by using == between two objects that I shouldn't be comparing, such as comparing a number to an array of numbers or comparing a string to a character. The == comparison evaluates to false, but it wasn't the comparison I meant to make. For example,

a = [1,2,3]
b = 5


if a == b; # evaluates to false

I wish there were an equality-checking operator that would give an error for incompatible types, like in the example above. I'm not sure if the right implementation would be

  • check if they have exactly the same type
  • check some <: subtyping relationship
  • check if they can convert into each other's types
  • check if they can promote into a shared type

or something else, or multiple different operators for different purposes. There are plenty of equals-like operators in unicode (eg ≟, ≐). Regardless, the current situation where my mistakes result in silent falses causes me problems too often for comfort; I would much rather get an explicit error that I can correct.

What would be a good solution here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    breakingThis change will break codeequalityIssues relating to equality relations: ==, ===, isequal

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions