Type-safe way to force caller to check Ok/Err Result values
See original GitHub issueHi, In the static languages that inspire this package, the default way of accessing values is only allowed once you’ve checked a Result is Ok:
return actuallyAnErr.value # type error!
However, in this package, because Ok and Err both share .value, this is by default allowed, and the user is not forced to check that a result is ok.
return actuallyAnError.value # fine, but will confuse the hell out of the caller because we just swallowed an error!
Could you consider “blessing” an accessor for value that only exists on Ok, to allow this usage? (Or rename .value on Err to .value_err or something, but that would be another big breaking change…)
Thanks for all your work on this package, I use it in a bunch of places.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:14 (2 by maintainers)
Top Results From Across the Web
Force SUM method to return NULL when applicable in EF Core
This method returns zero if source contains no elements. ... Or if you prefer that any NULL value results in a NULL returned...
Read more >Attributes interpreted by the C# compiler: Tracking caller ...
Determine caller information using attributes interpreted by the C# ... a return value, or a generic type parameter), this result is the ...
Read more >Can I force the caller of a function to process the return value?
I'm funneling exceptions through a result type, but this funneling is not of much use if the caller can ignore the result value...
Read more >Type-Safe TypeScript with Type Narrowing - Rainer Hahnekamp
First, we get rid of the possibility that the value is null or undefined via truthiness narrowing and return the value 0 (we'll...
Read more >Function.prototype.caller - JavaScript - MDN Web Docs - Mozilla
The caller accessor property of Function instances represents the function that ... Checking the value of a function's caller property.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Okay, so the proposal here and to be implemented in #99 is,
value
, deprecation warning for 1 or more releases, then raise exception on usage, then total removalvalue
anderr_value
properties that exist only onOk
andErr
respectivelyAny objections to this? Any better ideas?
I’ve taken the discussions here, because I’ve had the same annoyance with type checkers complaining about
Ok
being of typestr | None
instead ofstr
, and created a fork that is breaking compatibility with the upstream repo in many ways.https://github.com/felixhammerl/resultify
Since Python does not have the pattern matching capability of Rust, Python’s intepretation of
Optional
is not compatible with Rust’s and it does not quite make sense in my opinion to treat them equally.