question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type-safe way to force caller to check Ok/Err Result values

See original GitHub issue

Hi, 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:open
  • Created 2 years ago
  • Reactions:2
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
franciumcommented, Sep 12, 2022

Okay, so the proposal here and to be implemented in #99 is,

  • Deprecate value, deprecation warning for 1 or more releases, then raise exception on usage, then total removal
  • Add value and err_value properties that exist only on Ok and Err respectively

Any objections to this? Any better ideas?

2reactions
felixhammerlcommented, Jul 6, 2021

I’ve taken the discussions here, because I’ve had the same annoyance with type checkers complaining about Ok being of type str | None instead of str, 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found