Skip to content

[API suggestion] Avoid ~dummy flag in favour of Obj.magic #1

@craigfe

Description

@craigfe

Thanks for this excellent library :-)

I have a suggestion for the API that I'm sure you have considered, but I thought I'd propose it anyway to hear your thoughts on the matter. I have two reasons for disliking the ~dummy idiom:

  • it can be somewhat heavy in practice, since the obligation can cascade up the stack to other APIs / users whenever a vector is used polymorphically;
  • it places some artificial limits on how the API can be used. For instance, I can't construct a vector of values that contain backpointers to their parent:
type scheduler = { ...; vector: request Vector.t } (* some type with no trivial element *)
and request = { ...; parent: scheduler }

let create_scheduler () = { ...; Vector.create ~dummy:(__doesn't_yet_exist__) 0 }

What do you think of using e.g. Obj.magic None as the dummy value internally, and dropping the requirement for this parameter? I suspect the necessary internal invariants are not too hard to enforce (as the signature already never returns dummy values except for the unsafe_ functions), and am happy to contribute a patch if so.

This would unfortunately prevent taking advantage of implicit float arrays, although these seem to be going out of fashion lately (ocaml/ocaml#1294). To recover the compact float representation, it'd probably be necessary to go the route of Floatarray and make a dedicated Vector.Float module.

Thanks for your time :-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions