# "Nullable" (really "Nothingable") Types

**URL:** https://discourse.julialang.org/t/nullable-really-nothingable-types/55894
**Category:** General Usage
**Tags:** macros
**Created:** [February 23, 2021, 10:29pm UTC](https://discourse.julialang.org/t/nullable-really-nothingable-types/55894 "2021-02-23T22:29:56Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![astrobc1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/astrobc1/32/45632_2.png) [@astrobc1](https://discourse.julialang.org/u/astrobc1)
#### Post date: [February 23, 2021, 10:29pm UTC](https://discourse.julialang.org/t/nullable-really-nothingable-types/55894/1 "2021-02-23T22:29:56Z")

</div>

I’m curious if there’s a proper Julian way to implement optional struct fields through an effective Union{T, Nothing}.

I’ve written the macro below and it seems to work for my very simple test case. Is there a better way to achieve this? I suppose there could also be a macro for specific fields instead of the whole struct.

```julia
macro optional(block::Expr)
    struct_name = block.args[2]
    fields_expr = block.args[3].args
    fields = Expr[]
    for field ∈ fields_expr
        if typeof(field) !== LineNumberNode
            s = field.args[1]
            t = field.args[2]
            new_field = :($(s)::Union{$(t), Nothing})
            push!(fields, new_field)
        end
    end
    esc(quote
            struct $struct_name
                $(fields...)
            end
    end)
end

@optional struct A
    x::Float64
    y::Float64
end

a = A(1.0, nothing)
println(a)

```

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [February 23, 2021, 10:41pm UTC](https://discourse.julialang.org/t/nullable-really-nothingable-types/55894/2 "2021-02-23T22:41:56Z")

</div>

One other option would be something like

```julia
julia> macro var"?"(T)
           esc(:(Union{$T, Nothing}))
       end
@? (macro with 1 method)

julia> struct A
           x:: @? Float64
           y:: Float64
       end

julia> f(x::@? A) = x
f (generic function with 1 method)

julia> f(nothing)

julia> f(A(nothing, 1))
A(nothing, 1.0)

```

---

<div class="post-metadata">

### Author: ![astrobc1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/astrobc1/32/45632_2.png) [@astrobc1](https://discourse.julialang.org/u/astrobc1)
#### Post date: [February 23, 2021, 10:47pm UTC](https://discourse.julialang.org/t/nullable-really-nothingable-types/55894/3 "2021-02-23T22:47:29Z")

</div>

Welp, I’m sold. That does sound like the best route! I didn’t even know one can use strings to define methods like you just did… Crazy stuff. Thanks!

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [February 23, 2021, 11:27pm UTC](https://discourse.julialang.org/t/nullable-really-nothingable-types/55894/4 "2021-02-23T23:27:57Z")

</div>

One thing to note is that the `var` stringmacro was introduced in version `1.3`, but it’s a oneliner to define your own version:

```julia
julia> macro myvar_str(s) esc(Symbol(s)) end;

julia> myvar"a multiword variable name" = 1
1

julia> myvar"a multiword variable name"
1

```
