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 ?
Ah, that may be all I need! The IDs being global between controls is more or less what I want right now. There’s a later part of the system that erases the specifics with object subtyping, and operates on generic controls, but still needs IDs. The IDs are mostly to say “these are semantically the same element”. There’s a diff between two control layouts and the IDs help track what changed.
No PPX jock I. But I remember there was an example using “getenv”, where the getenv happened at compile-time. I can only imagine that it would be child’s play to concatenate process-id and current-time-in-nanos to get a unique ID, again at compile-time.