I have some objects that I want to give a compile time id, something like:
I’d like a way to use integers if possible, but have the ergonomics of strings/polymorphic variants. The polymorphic variants seem to pollute the code with a lot of type variables to hold the list of used IDs (I’m not sure of a good way to hide, then compare them?). And I’d like to avoid doing string comparisons between IDs all the time.
Slightly more example code:
type obj1 = < id : int; (* Ideally, or something opaque and simple *) text : string; > type obj2 = < id : int; text : string; click : unit -> unit; > let obj1 : obj1 = Text.create ~id:`TxtHello ~text:"Hello World" in let obj2 : obj2 = Button.create ~id:`BtnClick ~text:"Click" ~handler:(fun ...) in ...
I’d also like to avoid having to pre-allocate a bunch of id’s and avoid having a giant variant with all possible ids. Is there any kind of
variant -> int ?