# Can't pass \`nothing\` to macro

**URL:** <https://discourse.julialang.org/t/cant-pass-nothing-to-macro/15468>\
**Category:** General Usage\
**Tags:** macros\
**Created:** [September 25, 2018, 2:01pm UTC](https://discourse.julialang.org/t/cant-pass-nothing-to-macro/15468 "2018-09-25T14:01:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 25, 2018, 2:01pm UTC](https://discourse.julialang.org/t/cant-pass-nothing-to-macro/15468/1 "2018-09-25T14:01:35Z")

</div>

I am running julia 1.0 on Manjaro Linux. I have created a macro like this, which can generate a power function, with optionally a constant offset and/or a constant factor:

```julia
macro pow(functionname::Symbol, pow::Int, add::Union{Nothing,Number}=nothing, fact::Union{Nothing,Number}=nothing)
	expr = :(x ^ $pow)
	if !(fact === nothing)
		expr = :($fact * $expr)
	end
	if !(add === nothing)
		expr = :($expr + $add)
	end
	return quote
		$(esc(functionname))(x) = $expr
	end
end

```

This works pretty well and I can call it either like this: `@pow f 2`, this: `@pow f 2 1` or like this: `@pow f 2 1 .5`, which does exactly what it should.  
Now, I want the macro to generate a function without an offset but with a constant factor and I don’t want to just set the offset to 0, because it would still generate the extra code. I tried just calling it like this: `@pow f 2 nothing .5`, but it fails with this error:

```julia
ERROR: LoadError: MethodError: no method matching @pow(::LineNumberNode, ::Module, ::Symbol, ::Int64, ::Symbol, ::Float64)
Closest candidates are:
  @pow(::LineNumberNode, ::Module, ::Symbol, ::Int64, ::Union{Nothing, Number}, ::Union{Nothing, Number}) at /home/simeon/macro.jl:2
  @pow(::LineNumberNode, ::Module, ::Symbol, ::Int64) at /home/simeon/macro.jl:2
  @pow(::LineNumberNode, ::Module, ::Symbol, ::Int64, ::Union{Nothing, Number}) at /home/simeon/macro.jl:2
in expression starting at REPL[2]:1

```

, which seems to indicate that it interprets `nothing` as a Symbol and not a `Nothing`-Type.  
Is this behavior actually intentional? If so, is there a way to tell Julia not to interpret `nothing` as a symbol but as a value? If also tried it like this: `$nothing`, `nothing::Union{Nothing,Number}` and even with a function that just returns `nothing`, all of which failed. Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 25, 2018, 2:06pm UTC](https://discourse.julialang.org/t/cant-pass-nothing-to-macro/15468/2 "2018-09-25T14:06:52Z")

</div>

Macros work on syntax, so yes, `nothing` is just a symbol.

You can, of course special, case the interpretation of that symbol in your macro.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 25, 2018, 2:21pm UTC](https://discourse.julialang.org/t/cant-pass-nothing-to-macro/15468/3 "2018-09-25T14:21:37Z")

</div>

Yes, I realize, that I can just introduce a special case, but there are definitely use cases where you’d want to have an extra `nothing` case. Is there no way to do this? It works fine with basic types like Integers or Strings, is there no way to pass `nothing` like this, too? Or would you have to make `nothing` part of the actual syntax to do this?

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [September 25, 2018, 8:02pm UTC](https://discourse.julialang.org/t/cant-pass-nothing-to-macro/15468/4 "2018-09-25T20:02:16Z")

</div>

If you care about literal nothings, you can do

```julia
julia> macro foo(x)
       if x==:nothing
       return :1
       else
       return :($x)
       end
       end
julia> @foo 2+3
5

julia> @foo nothing
1

julia> @foo Nothing()

```

But as you see, this does not catch different spellings of `nothing`, which would need to be `@generated`.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [September 25, 2018, 8:09pm UTC](https://discourse.julialang.org/t/cant-pass-nothing-to-macro/15468/5 "2018-09-25T20:09:36Z")

</div>

This should also be performant if the type of `x` is known or inferred properly.

```julia
macro foo(x)
    esc(quote 
        if $x isa Nothing
	    return 1
        else
            return $x
        end
    end)
end

```
