Skip to content

Consider using Float.equal instead of = in [@@deriving eq] #298

@Niols

Description

@Niols

For simple types, [@@deriving eq] falls back on =:

| true, ([%type: int] | [%type: int32] | [%type: Int32.t] |
[%type: int64] | [%type: Int64.t] | [%type: nativeint] |
[%type: Nativeint.t] | [%type: float] | [%type: bool] |
[%type: char] | [%type: string] | [%type: bytes]) ->
[%expr (fun (a:[%t typ]) b -> a = b)]

It sounds reasonable, and in general is fine. In the very specific case of nan, though, it will reach the typical thing that nan <> nan. In the standard library, however, there is Float.equal that does equate nan with itself. I think we should use that one, because:

  • [@@deriving eq] behaves differently on type t = float and on type t = Float.t.
  • As long as a float appears in a structure, it does not hold that, with [@@deriving eq, ord], equal x y = (compare x y = 0).
  • eq argues that monomorphic equality is better (which, hell yeah), so one might expect Float.equal rather than a fallback on =. (This one is pretty subjective, I suppose.)

The big downside, today, would be that this introduces a breaking change; bummer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions