Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] checking for same ctor type?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Chris Quinn <cq@h...>
Subject: Re: [Caml-list] checking for same ctor type?
Pervasives.compare is fine for nullary constructors or where you don't mind the performance hit of the function recursing into the structure of the arguments.

A more efficient equality predicate that is really just a cut down version of [compare]:

module Kludge =
  struct
    let cmp (a: 't) (b: 't) =
      let a' = Obj.repr a and b' = Obj.repr b in
      match Obj.is_block a', Obj.is_block b' with
      | true,true ->
          Obj.tag a' = Obj.tag b'
      | false,false ->
          a' = b'
      | _ -> false
  end

It of course relies on the physical representation of sum types.
It has the merit of working on a type of any number of constructors, but the mis-merit of also operating over functions for instance!
To constrain such a comparison to sum types would require it to be built into the compiler.

Chris Q.
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr