# Equivalent of \`@something\` for field access

**URL:** <https://discourse.julialang.org/t/equivalent-of-something-for-field-access/129177>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 20, 2025, 11:48am UTC](https://discourse.julialang.org/t/equivalent-of-something-for-field-access/129177 "2025-05-20T11:48:49Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)\
**Post date:** [May 20, 2025, 11:48am UTC](https://discourse.julialang.org/t/equivalent-of-something-for-field-access/129177/1 "2025-05-20T11:48:49Z")

</div>

I really like the `@something` operation because it lets me write concise and readable code such as:

```julia
function get_operators(expr, operators=nothing)
    return @something(operators, get_metadata(expr).operators)
end

```

This is shorter, and I find it more readable and even more robust since it includes additional assertions `!== nothing` which I wouldn’t normally write.

I was wondering if there’s something similar for field access. Throughout my library I often have code like this:

```julia
y = isnothing(x) ? nothing : x.val
# OR
y = isnothing(x) ? nothing : x[i]

```

Essentially, it tries to perform a getproperty/getindex operation, but propagates nothing if the object itself is nothing.

Before writing my own utility function, I just wanted to check: am I missing something already available in Base? It took me a few years to discover `@something`, so I wouldn’t be surprised if there’s another built-in shorthand I’m overlooking.

---

<div class="post-metadata">

**Author:** ![tecosaur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tecosaur/32/23206_2.png) [@tecosaur](https://discourse.julialang.org/u/tecosaur)\
**Post date:** [May 20, 2025, 11:51am UTC](https://discourse.julialang.org/t/equivalent-of-something-for-field-access/129177/2 "2025-05-20T11:51:01Z")

</div>

Hmm, maybe it’s a bit too magical but I almost wouldn’t mind it if single-arg `@something f(x)` worked as `if !isnothing(x); f(x) end` (where `f` may also be `getfield`, `getindex`, etc.)

---

<div class="post-metadata">

**Author:** ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)\
**Post date:** [May 20, 2025, 11:51am UTC](https://discourse.julialang.org/t/equivalent-of-something-for-field-access/129177/3 "2025-05-20T11:51:41Z")

</div>

Yes! Something like

```julia
y = @maybe x.val

```

would be very nice.

---

<div class="post-metadata">

**Author:** ![tecosaur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tecosaur/32/23206_2.png) [@tecosaur](https://discourse.julialang.org/u/tecosaur)\
**Post date:** [May 20, 2025, 11:53am UTC](https://discourse.julialang.org/t/equivalent-of-something-for-field-access/129177/4 "2025-05-20T11:53:47Z")

</div>

It is a somewhat distinct idea, so maybe a new name like `@maybe` is worth it, but I’m really not sure why you’d ever use single-arg `@something` without it doing something like what I mention — at least I feel like it could be cool (worth mentioning at least).

Also, this is a code pattern I’ve run into a bunch myself, so FWIW it’s not just you getting fed up of manually writing `!isnothing` checks.

---

<div class="post-metadata">

**Author:** ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)\
**Post date:** [May 20, 2025, 11:55am UTC](https://discourse.julialang.org/t/equivalent-of-something-for-field-access/129177/5 "2025-05-20T11:55:15Z")

</div>

I guess I feel like `@something` should always return something that `!== nothing`, with the sole exception being if you wrap it with `Some`. So a single arg version feels a bit semantically uncomfy. Idk. Maybe even `y = @safe x.val` could be nice.

---

<div class="post-metadata">

**Author:** ![tecosaur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tecosaur/32/23206_2.png) [@tecosaur](https://discourse.julialang.org/u/tecosaur)\
**Post date:** [May 20, 2025, 11:56am UTC](https://discourse.julialang.org/t/equivalent-of-something-for-field-access/129177/6 "2025-05-20T11:56:39Z")

</div>

> [@MilesCranmer](#):
>
> I guess I feel like `@something` should always return something that `!== nothing`

Oh yes, good point, I’m with you. Obviously it’s not an option here, but this kind of reminds me of syntax in other languages like `thing?.property` where nulls/errors are propagated before `.property` is hit.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [May 20, 2025, 11:59am UTC](https://discourse.julialang.org/t/equivalent-of-something-for-field-access/129177/7 "2025-05-20T11:59:46Z")

</div>

See:

```julia
using AccessorsExtra

@oget obj.a
@oget obj.a.b
@oget first(obj.a)

```

returns `nothing` if it encounters `nothing` at any step, or the property/… doesn’t exist. Works oob with many objects and accessors.  
Also:

```julia
@oget obj.a.b obj.c.d 123

```

does what you expect from `@something` – goes left to right until finds something.

* * *

`@maybe` also exists, but creates a function like `@optic` in Accessors.jl but optional.

---

<div class="post-metadata">

**Author:** ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)\
**Post date:** [May 20, 2025, 12:08pm UTC](https://discourse.julialang.org/t/equivalent-of-something-for-field-access/129177/8 "2025-05-20T12:08:41Z")

</div>

Nice! I like that a lot. Also seems like it has no overhead which is great

```julia-repl
julia> function foo(x)
           return @oget x.a.b
       end
foo (generic function with 1 method)

julia> x = (; a=(; b=1))
(a = (b = 1,),)

julia> @code_llvm foo(x)
; Function Signature: foo(NamedTuple{(:a,), Tuple{NamedTuple{(:b,), Tuple{Int64}}}})
; @ REPL[9]:1 within `foo`
define i64 @julia_foo_14575(ptr nocapture noundef nonnull readonly align 8 dereferenceable(8) %"x::NamedTuple") #0 {
top:
; @ REPL[9]:2 within `foo`
; ┌ @ /Users/mcranmer/.julia/packages/AccessorsExtra/4VGss/src/maybe.jl:201 within `macro expansion`
   %"x::NamedTuple.a_ptr.b_ptr.unbox" = load i64, ptr %"x::NamedTuple", align 8
   ret i64 %"x::NamedTuple.a_ptr.b_ptr.unbox"
; └
}

```

If there’s any appetite to put some of that into Base I’d put my hat in the ring.

---

<div class="post-metadata">

**Author:** ![tecosaur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tecosaur/32/23206_2.png) [@tecosaur](https://discourse.julialang.org/u/tecosaur)\
**Post date:** [May 20, 2025, 12:09pm UTC](https://discourse.julialang.org/t/equivalent-of-something-for-field-access/129177/9 "2025-05-20T12:09:57Z")

</div>

Neat! This reminds me of how much I’d like it if [generalised references](https://www.lispworks.com/documentation/HyperSpec/Body/05_a.htm) (think `@optic`) were a feature you got with Julia OOTB.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [May 20, 2025, 12:40pm UTC](https://discourse.julialang.org/t/equivalent-of-something-for-field-access/129177/10 "2025-05-20T12:40:37Z")

</div>

> [@MilesCranmer](#):
>
> Also seems like it has no overhead which is great

That’s a key part of Accessors.jl philosophy in general 🙂

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [May 20, 2025, 12:43pm UTC](https://discourse.julialang.org/t/equivalent-of-something-for-field-access/129177/11 "2025-05-20T12:43:51Z")

</div>

> [@tecosaur](#):
>
> This reminds me of how much I’d like it if [generalised references](https://www.lispworks.com/documentation/HyperSpec/Body/05_a.htm) (think `@optic`) were a feature you got with Julia OOTB.

Optics are already part of a lightweight, stable, and very popular Accessors.jl 🙂 I like the Julian approach of very lightweight Base.

These “optional” optics (incl `@maybe`, `@oget`) can in general be upstreamed there from AccessorsExtra.jl, it just needs a more careful consideration of the api surface and edgecases. I never really came to do this.

Actually, _maybe_ they shouldn’t be upstreamed to Accessors proper, but to a separate AccessorsMaybe package?.. The full AccessorsExtra.jl has a lot more stuff and I don’t focus on keeping it lightweight.

---

<div class="post-metadata">

**Author:** ![MilesCranmer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milescranmer/32/21070_2.png) [@MilesCranmer](https://discourse.julialang.org/u/MilesCranmer)\
**Post date:** [May 20, 2025, 1:20pm UTC](https://discourse.julialang.org/t/equivalent-of-something-for-field-access/129177/12 "2025-05-20T13:20:31Z")

</div>

> [@aplavin](#):
>
> Actually, _maybe_ they shouldn’t be upstreamed to Accessors proper, but to a separate AccessorsMaybe package?.. The full [AccessorsExtra.jl](https://juliaregistries.github.io/General/packages/redirect_to_repo/AccessorsExtra) has a lot more stuff and I don’t focus on keeping it lightweight.

I think that would be a great idea.

In my libraries, I try to minimise dependencies, particularly those that aren’t (yet) widely used. Small quality-of-life features (especially ones stable enough to rarely need updates) I tend to stick to only the ones from Base. Not only to reduce dependency management, but also as a type of language standardization, so that newcomers don’t have to learn different syntaxes for every utility library I am using. e.g., `@something` is in Julia itself so it isn’t too exotic for someone to encounter in my codebase.

However, if a dependency is well-contained and unambitious, I feel much more comfortable relying on it.

All of this is to say that, yes, I think AccessorsMaybe would be a great idea.

---

<div class="post-metadata">

**Author:** ![tecosaur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tecosaur/32/23206_2.png) [@tecosaur](https://discourse.julialang.org/u/tecosaur)\
**Post date:** [May 20, 2025, 2:55pm UTC](https://discourse.julialang.org/t/equivalent-of-something-for-field-access/129177/13 "2025-05-20T14:55:23Z")

</div>

> [@aplavin](#):
>
> I like the Julian approach of very lightweight Base.

Light is generally pretty good, but in my mind at least I see just the basic but extensible interface as the sort of thing that could be pretty lightweight.

That said, these days package extensions are great for making it possible to have an optional implementation of an interface in a package.

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [May 20, 2025, 7:41pm UTC](https://discourse.julialang.org/t/equivalent-of-something-for-field-access/129177/14 "2025-05-20T19:41:16Z")

</div>

I’d love to have this syntax supported in Base. We have the `Union{T, Nothing}` pattern all over the place and it’s well-established as a pattern in other languages. It would be super convenient to be able to do `x?.y?.z?.a` and have the nothings propagate.

---

<div class="post-metadata">

**Author:** ![tecosaur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tecosaur/32/23206_2.png) [@tecosaur](https://discourse.julialang.org/u/tecosaur)\
**Post date:** [May 22, 2025, 2:53pm UTC](https://discourse.julialang.org/t/equivalent-of-something-for-field-access/129177/15 "2025-05-22T14:53:09Z")

</div>

> [@quinnj](#):
>
> I’d love to have this syntax supported in Base. We have the `Union{T, Nothing}` pattern all over the place and it’s well-established as a pattern in other languages.

Funnily enough, despite bringing up the `?.` syntax, I’m actually _not_ a fan of introducing it to Julia 😛 — I’m still pining for the ability to use `?` in identifiers like `!` ([#22025](https://github.com/JuliaLang/julia/issues/22025)).

There’s also the competing proposal to make `T?` sugar for `Union{T, Missing}` ([#36628](https://github.com/JuliaLang/julia/issues/36628)).

If we _did_ have an error-value system like Zig/Rust I think having a dedicated syntax would be justified (this or some similar syntax), but I don’t think we’re quite there in Julia.

That said, I would very much like something to make this pattern easier, and this seems like something that could easily be accomplished with a macro. It occurs to me that the name `@passnothing` is pretty explicit, but maybe not terse enough to provide the sort of convenience that Jacob is looking for?
