-
Notifications
You must be signed in to change notification settings - Fork 182
[#2141] Use optionals for return values that may be null to enforce a client check #2144
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
base: master
Are you sure you want to change the base?
[#2141] Use optionals for return values that may be null to enforce a client check #2144
Conversation
abd78d8 to
1d9ce27
Compare
|
In line with keeping PRs small and working on issues incrementally, I have decided to stop working on the implementation of Future PRs will look into the implementation of |
|
Just an initial thought: It would be nice if exceptions were part of the Failable optional type (e.g FailableOptional<LocalDateTime, DateTimeParseException>) which indicates that there exists the possibility of a throw which is still unhandled. Composing FailableOptionals with different unhandled exceptions should produce a FailableOptional with the sum type of these exceptions (this may be hard to do with Java 8/11). Handling the error, then gets rid of the exception from the type. Inspiration for monads of this kind is from Scala Zio and effect-ts. |
|
Hi, |
|
Hi, |
|
This PR was closed because it has been marked as stale for 7 days with no activity. |
|
Hi, |
|
This PR was closed because it has been marked as stale for 7 days with no activity. |
|
Hi, |
|
This PR was closed because it has been marked as stale for 7 days with no activity. |
Part of #2141
Proposed commit message
Other information
Main Rationale Behind
FailableOptionalMonadCurrently, there are chunks of code whereby
nulls are used as a flag or are part of a process whereby exceptions are thrown or messages are logged. The built-in JavaOptionalclass (in Java 8) does not support some of the operations or require lengthy workarounds to replicate the behaviour we want directly usingOptional.For example, consider
CommitInfoAnalyzer::analyzeCommit:Using
nullin this manner is dangerous ifDateTimeParseExceptionis thrown, sincedatewould have the valuenull, and the subsequent invocation ofdate.withZoneSameInstantwould fail due to aNullPointerException.Optionalcould be used here, but it is rather clunky becauseOptional's interface does not handle the production of values that might throw exceptions. Moreover, the other mapping and filtering methods do not allow lambdas or methods which throw exceptions.The use of
FailableOptionalcan help solve this, while mitigating the risk of invoking methods onnull, by wrapping up the function call that potentially throws an exception, and working with it as if it had not thrown, shielding the user from the complexity of handling thenullvalues.Consider this rewritten chunk of code:
This following implementation would not be subjected to the above buggy behaviour, as the potentially throwing method is wrapped into a
Fail-ed instance ofFailable, and subsequently recovered into a valid defaultLocalDateTimevalue.So far,
Failablesupports the usual operations thatOptionalcan do (identity (of,ofNullable), mapping (map,flatmap) and filtering (filter)), and support new operations that might be useful in our codebase (handling methods that might throw exceptions when executed but otherwise returns a value, etc.).So far, the following methods have been implemented:
Identity
ofofNullablesuccessemptyfailisPresentisAbsentisFailedMapping
mapunfailableMapflatMapunfailableFlatMapFiltering
filterGetters
getorElseMisc. methods
ifPresentifAbsentifFailresolveifFailThenThrowfailWithHere are some of the main processes that we need to complete to close out this Issue/PR. The relevant PRs which fulfil the tasks are included in the parenthesis following the task name below:
Failablemonad (#2144)Failable(#2144)Failablecan be used (#2144)Failablein key areas identified (#2144)