# Parse constant from enviroment - macro parameter string interpolation

**URL:** <https://discourse.julialang.org/t/parse-constant-from-enviroment-macro-parameter-string-interpolation/81133>\
**Category:** New to Julia\
**Tags:** macros, metaprogramming, string-interpolation\
**Created:** [May 16, 2022, 6:37am UTC](https://discourse.julialang.org/t/parse-constant-from-enviroment-macro-parameter-string-interpolation/81133 "2022-05-16T06:37:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![FireCrumb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/firecrumb/32/35370_2.png) [@FireCrumb](https://discourse.julialang.org/u/FireCrumb)\
**Post date:** [May 16, 2022, 6:37am UTC](https://discourse.julialang.org/t/parse-constant-from-enviroment-macro-parameter-string-interpolation/81133/1 "2022-05-16T06:37:15Z")

</div>

I have some global constants which I want to read from ENV or define with a default value.

I pass a parameter to the macro and want to put it into a string in the output expression.

When I use `"$($name)"` then I get `"$(VALUE)"`  
When I use `"$(name)"` then I get `"$(name)"`  
I want to to get back `"VALUE"`

* * *

Define constant directly:

```julia
const CONST1 = UInt16(10)

```

Parse value from ENV or default from code:

```julia
const CONST2 = parse(UInt16, get(ENV, "CONST2", "20"))

```

Automate using macro - can’t get it to work…

```julia
macro const_from_env(const_name::Symbol, const_type::Symbol, default_value)
    ex = quote
        const $(const_name) = parse($(const_type),
            get(ENV, "$($const_name)", "$($default_value)"))
    end
    @show ex
    esc(ex)
end

@const_from_env CONST3 UInt16 30

```

actual output:

```julia
ex = quote
    const CONST3 = parse(UInt16, get(ENV, "$(const_name)", "$(30)"))
end
ERROR: LoadError: UndefVarError: const_name not defined

```

desired output:

```julia
ex = quote
    const CONST3 = parse(UInt16, get(ENV, "CONST3", "30"))
end

```

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [May 16, 2022, 6:46am UTC](https://discourse.julialang.org/t/parse-constant-from-enviroment-macro-parameter-string-interpolation/81133/2 "2022-05-16T06:46:18Z")

</div>

I think you need `$$` rather than `$` and

```julia
quote quote … end end

```

See  
[https://docs.julialang.org/en/v1/manual/metaprogramming/#Nested-quote](https://docs.julialang.org/en/v1/manual/metaprogramming/#Nested-quote)

---

<div class="post-metadata">

**Author:** ![FireCrumb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/firecrumb/32/35370_2.png) [@FireCrumb](https://discourse.julialang.org/u/FireCrumb)\
**Post date:** [May 16, 2022, 7:36am UTC](https://discourse.julialang.org/t/parse-constant-from-enviroment-macro-parameter-string-interpolation/81133/3 "2022-05-16T07:36:54Z")

</div>

> [@jd-foster](#):
>
> I think you need `$` rather than `$` and
> 
> ```julia
> quote quote … end end
> 
> ```

```julia
quote quote
        const $(const_name) = parse($(const_type),
            get(ENV, "$$const_name", "$$default_value"))
    end end

```

gives error  
`ERROR: LoadError: syntax: invalid interpolation syntax: "$$"`

---

<div class="post-metadata">

**Author:** ![FireCrumb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/firecrumb/32/35370_2.png) [@FireCrumb](https://discourse.julialang.org/u/FireCrumb)\
**Post date:** [May 16, 2022, 8:01am UTC](https://discourse.julialang.org/t/parse-constant-from-enviroment-macro-parameter-string-interpolation/81133/4 "2022-05-16T08:01:18Z")

</div>

The solution is `$("$name")`.

Full code:

```julia
macro const_from_env(const_name::Symbol, const_type::Symbol, default_value)
    esc(quote
        const $(const_name) = parse($(const_type),
            get(ENV, $("$const_name"), $("$default_value")))
    end)
end

@const_from_env CONST3 UInt16 30
@assert 30 == CONST3

```
