Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional thunk #170

Merged
merged 4 commits into from
Apr 3, 2020
Merged

Optional thunk #170

merged 4 commits into from
Apr 3, 2020

Conversation

seliopou
Copy link
Member

@seliopou seliopou commented Apr 2, 2020

Introduce an Optional_thunk.t type to replace most of the Sys.opaque_identities used throughout the code. This type requires one to check whether it is "none" or "some" before using it as a regular function. The unchecked_value function reminds you that you might be performing an unsafe operation.

seliopou added 4 commits April 2, 2020 18:23
This module implements an option value specialized for thunks. It uses
the identity function as a special value to represent none, which means
that a "some" value cannot be constructed using the identity function.
This is just used for the ready_to_write callback. The read callbacks
are already guarded by a bool.
@seliopou seliopou requested a review from dpatti April 2, 2020 22:42
@seliopou
Copy link
Member Author

seliopou commented Apr 2, 2020

@Lupus can you take this for a spin?

@Lupus
Copy link
Contributor

Lupus commented Apr 3, 2020

Just checked this branch against our test suite - it works just fine, as it used before the #161! 🎉 Thanks for quick turnaround!

@seliopou seliopou merged commit fdda189 into master Apr 3, 2020
@seliopou seliopou deleted the optional-thunk branch April 3, 2020 11:40

let is_none t = t == none
let is_some t = not (is_none t)
let unchecked_value t = t
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about this when I was reviewing it and asking myself "should this be raising if t is none?" – but that felt a little burdensome in all of the places we use it safely.

But now I'm thinking about it some more, and I'm wondering: maybe it would make sense if we split this into two functions, kind of like what we talked about before the PR:

(** Return the exposed thunk, raises if called on [none] *)
val value_exn : t -> unit -> unit

(** Invoke the thunk if [some], do nothing if [none] *)
val invoke_if_some : t -> unit

This has two benefits: calling an optional thunk should always be safe, since the default is to just noop by invoking none, and we can use this in all of the current places where we use unchecked_value without is_some. But removing opaqueness is something you have to be a bit more careful about, since presumably you should be able to do Optional_thunk.some (Optional_thunk.value_exn t). And ultimately I rank "descriptive names" below "failing at runtime" and "compiler error" in terms of safety.

anmonteiro added a commit to anmonteiro/ocaml-h2 that referenced this pull request Apr 6, 2020
anmonteiro added a commit to anmonteiro/ocaml-h2 that referenced this pull request Apr 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants