Skip to content
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

@something: a short-circuiting something #40729

Merged
merged 3 commits into from
May 11, 2021
Merged

@something: a short-circuiting something #40729

merged 3 commits into from
May 11, 2021

Conversation

omus
Copy link
Member

@omus omus commented May 6, 2021

In the AWS.jl package I was noticing a pattern where if an argument was undefined a series of fallbacks would be tried. The code was similar to:

if region === nothing
    region === nothing && (region = get(ENV, "AWS_REGION", nothing)
    region === nothing && (region = get_profile_region())
    region === nothing && (region = get_ec2_instance_region())
    region === nothing && (region = DEFAULT_REGION)
end

Since the code above makes use of nothing using the something function is a natural fit:

something(
    region,
    get(ENV, "AWS_REGION", nothing),
    get_profile_region(),
    get_ec2_instance_region(),
    DEFAULT_REGION,
)

This is much easier to read but unfortunately evaluates all arguments even if they aren't used. The @something macro is equivalent to something but skips evaluating all argument after the first non-nothing value.

@omus omus added the needs news A NEWS entry is required for this change label May 6, 2021
@omus
Copy link
Member Author

omus commented May 6, 2021

I'll add a NEWS entry tomorrow

@jw3126
Copy link
Contributor

jw3126 commented May 6, 2021

Nice! Would it make sence to also have @coalesce?

@omus
Copy link
Member Author

omus commented May 6, 2021

Nice! Would it make sence to also have @coalesce?

That can definitely be added. I just didn't have a use case for it

@omus
Copy link
Member Author

omus commented May 7, 2021

  • Added NEWS entry
  • Added short-circuiting @coalesce
  • Fixed a minor documentation error which made it appear that something and coalesce required at least one argument.

@omus omus added missing data Base.missing and related functionality and removed needs news A NEWS entry is required for this change labels May 7, 2021
@omus
Copy link
Member Author

omus commented May 7, 2021

CI failure is from the LibCURL stdlib and is unrelated

@omus omus merged commit 6cebd28 into master May 11, 2021
@omus omus deleted the cv/at-something branch May 11, 2021 14:04
@ararslan
Copy link
Member

Ah, I forgot to mention that these should have a compat annotation in the docstrings.

@omus
Copy link
Member Author

omus commented May 14, 2021

Ah, I forgot to mention that these should have a compat annotation in the docstrings.

I'll take care of that now

@omus omus removed the needs compat annotation Add !!! compat "Julia x.y" to the docstring label May 14, 2021
bors bot added a commit to JuliaCloud/AWS.jl that referenced this pull request May 27, 2021
371: Use external `@something` r=mattBrzezinski a=omus

Replaces the definition of `@something` with the one [from base](JuliaLang/julia#40729) or [from Compat.jl](JuliaLang/Compat.jl#742).

Co-authored-by: Curtis Vogt <curtis.vogt@gmail.com>
shirodkara pushed a commit to shirodkara/julia that referenced this pull request Jun 9, 2021
* Create `@something`

* Create `@coalesce`

* Correction to `something`/`coalesce` docstring
johanmon pushed a commit to johanmon/julia that referenced this pull request Jul 5, 2021
* Create `@something`

* Create `@coalesce`

* Correction to `something`/`coalesce` docstring
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
missing data Base.missing and related functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants