-
Notifications
You must be signed in to change notification settings - Fork 44
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
Optional thunk #170
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
09c3774
optional-thunk: implement an optional thunk module
seliopou 367db01
optional-thunk: use Optional_thunk in Server_connection
seliopou a28322b
optional-thunk: Use Optional_thunk in body
seliopou 01f7bcf
optional-thunk: use Optional_thunk in Reqd
seliopou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
type t = unit -> unit | ||
|
||
let none = Sys.opaque_identity (fun () -> ()) | ||
let some f = | ||
if f == none | ||
then failwith "Optional_thunk: this function is not representable as a some value"; | ||
f | ||
|
||
let is_none t = t == none | ||
let is_some t = not (is_none t) | ||
let unchecked_value t = t | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
type t | ||
|
||
val none : t | ||
val some : (unit -> unit) -> t | ||
|
||
val is_none : t -> bool | ||
val is_some : t -> bool | ||
|
||
val unchecked_value : t -> unit -> unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
isnone
?" – 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:
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 useunchecked_value
withoutis_some
. But removing opaqueness is something you have to be a bit more careful about, since presumably you should be able to doOptional_thunk.some (Optional_thunk.value_exn t)
. And ultimately I rank "descriptive names" below "failing at runtime" and "compiler error" in terms of safety.