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.

Consider `Maybe` methods that state if a value is present/absent

See original GitHub issue

Hi,

I’m just getting my feet wet with returns. As a first iteration, I’m trying to introduce Maybe into a somewhat larger codebase of mine.

One thing that irks me a bit is code like this:

Coming from code like this:

item: Optional[Item] = find_item(item_id)
if item is None:
    raise SomeCustomException("ohai")

(Naively) porting to Maybe leaves me with this:

item: Maybe[Item] = find_item(item_id).value_or(None)
if item is None:
    raise SomeCustomException("ohai")

While I can replace is not None checks after which I want to process the value with Maybe.map (and not raise an exception if the value is missing) and gain more succinct code, almost the opposite seems to be the case if I don’t want to work with the potential value but rather its existence itself.

Another example:

# traditional
token: Optional[Token] = find_token(some_key)
valid_token_found: bool = (token is not None) and not token.is_expired

# `returns`-y way
from returns.maybe import Some
token: Maybe[Token] = find_token(some_key)
valid_token_found: bool = token.map(lambda t: not t.is_expired) is Some

What I’m looking for is a method like [is_]{present, absent, some, none}, has_value, empty, etc. to allow me to do this:

# `returns`-y way
# NB: No import necessary!
token: Maybe[Token] = find_token(some_key)
valid_token_found: bool = token.map(lambda t: not t.is_expired).present

Am I overlooking some best practice that avoids imports and identity/equality operators? If not, are you open to consider adding such (potentially even more auto-complete-friendly) methods?

Thanks!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
homeworkprodcommented, Mar 22, 2020

Hi @sobolevn, thank you for your quick response.

My intent is not to override is for this use case.

While is_successful technically works, I see a few downsides:

  • It requires an import to use it.
  • It doesn’t auto-complete. Both make it relatively hard to use for someone unfamiliar with returns.

From my experience with introducing Optional (both Guava’s and Java 8’s) into multiple rather big Java code bases I’ve learned that acceptance with people is higher if they don’t have to figure out too much. Pressing the auto-complete hotkey usually worked well; but having them to know to import something, and what exactly, and to do actually that would be a burden resulting in animosity towards the introduction of such a data structure and concept, and developers trying to introduce it 😃

I’m not requesting something super special here, but rather something I’m familiar with from other environments. For example:

0reactions
sobolevncommented, May 31, 2020

or_else_call sounds great! 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

4. Methods Use Instance Variables: How Objects Behave
State affects behavior, behavior affects state. We know that objects have state and behavior, represented by instance variables and methods.
Read more >
How to Deal with Missing Data - Master's in Data Science
Missing data can skew anything for data scientists, from economic analysis to clinical trials. After all, any analysis is only as good as...
Read more >
Data Cleaning with Python and Pandas: Detecting Missing ...
Now that we've worked through the different ways of detecting missing values, we'll take a look at summarizing, and replacing them. Summarizing ...
Read more >
Map (Java Platform SE 8 ) - Oracle Help Center
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. This...
Read more >
Types of scales & levels of measurement
There is no value possible below 0 degrees Kelvin, it is absolute zero. Weight is another example, 0 lbs. is a meaningful absence...
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