# How to include into local scope?

**URL:** <https://discourse.julialang.org/t/how-to-include-into-local-scope/34634>\
**Category:** General Usage\
**Created:** [February 14, 2020, 3:58pm UTC](https://discourse.julialang.org/t/how-to-include-into-local-scope/34634 "2020-02-14T15:58:15Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 14, 2020, 5:19pm UTC](https://discourse.julialang.org/t/how-to-include-into-local-scope/34634/21 "2020-02-14T17:19:37Z")

</div>

> [@HenrikM](#):
>
> `type QuoteNode has no field file`

That is an error in @oxinabox’s macro. Use the one in [my post](https://discourse.julialang.org/t/how-to-include-into-local-scope/34634/11) instead.

---

<div class="post-metadata">

**Author:** ![HenrikM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrikm/32/17975_2.png) [@HenrikM](https://discourse.julialang.org/u/HenrikM)\
**Post date:** [February 14, 2020, 5:19pm UTC](https://discourse.julialang.org/t/how-to-include-into-local-scope/34634/22 "2020-02-14T17:19:42Z")

</div>

> [@stevengj](#):
>
> It sounds like you have default values for those parameters

I don’t.

---

<div class="post-metadata">

**Author:** ![jbrea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbrea/32/3879_2.png) [@jbrea](https://discourse.julialang.org/u/jbrea)\
**Post date:** [February 14, 2020, 5:24pm UTC](https://discourse.julialang.org/t/how-to-include-into-local-scope/34634/23 "2020-02-14T17:24:57Z")

</div>

> [@HenrikM](#):
>
> I think this is just syntax. The semantics of `wowPlot` are still the same and still too broad to be a good function.

I agree, this is just syntax. I thought you were worried about the many function arguments.

But I don’t have a problem with functions that have quite broad semantics. This happens actallually quite often with plotting functions. How many arguments (including keyword arguments) can you pass to Plots.plot?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 14, 2020, 5:25pm UTC](https://discourse.julialang.org/t/how-to-include-into-local-scope/34634/24 "2020-02-14T17:25:19Z")

</div>

> [@HenrikM](#):
>
> I don’t.

You wrote “Currently I just copy-paste the plotting code to all places in all scripts where I need it.”

Either you are changing all of those parameters every time you run your script, in which case passing all of the keywords is an equivalent amount of handwritten code while being more readable, or you are re-using some of the parameter settings, in which case you only need to pass some of the keywords.

Look, I get it — transitioning from copy-paste code to re-usable code is hard to learn, hard to do, and hard to get into the habit of. There are no magic words we can say that will make this process easy; all we can do is to provide a few hints. And yes, one can do `@include` tricks in Julia to partially automate copy-and-pasting, but in the long run this is not a good substitute for re-thinking how you approach re-usable code. (I would recommend [this language-agnostic book](https://www.amazon.com/Pragmatic-Programmer-journey-mastery-Anniversary/dp/0135957052) on the subject.)

---

<div class="post-metadata">

**Author:** ![HenrikM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrikm/32/17975_2.png) [@HenrikM](https://discourse.julialang.org/u/HenrikM)\
**Post date:** [February 14, 2020, 5:34pm UTC](https://discourse.julialang.org/t/how-to-include-into-local-scope/34634/25 "2020-02-14T17:34:43Z")

</div>

I totally agree with you. Thank all of you for your answers!

I’m doing scientific software developing of the dirtiest kind, if you know what I mean. I have dozens of scripts that do similar but not identical things and there is a lot of MCMC, so all parameters change, but not all at once. My humble wish was just that I could do my dirty work quicker. Your macro-hack seems to be just what I wanted. If and when my code is correct and useful I will refactor it, I promise.

PS. I think @oxinabox’s macro worked before he over-engineering it.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 14, 2020, 5:38pm UTC](https://discourse.julialang.org/t/how-to-include-into-local-scope/34634/26 "2020-02-14T17:38:28Z")

</div>

Yes, I’m saying that is better than having all those parameters scattered across a file and not even appear together.

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [February 14, 2020, 6:15pm UTC](https://discourse.julialang.org/t/how-to-include-into-local-scope/34634/27 "2020-02-14T18:15:21Z")

</div>

> [@HenrikM](#):
>
> And then unpack inside the function? I think this is just syntax.

Syntactically, named tuples + the `Parameters.jl` package can be clean and have fewer errors. See [4. Arrays, Tuples, Ranges, and Other Fundamental Types — Quantitative Economics with Julia](https://julia.quantecon.org/getting_started_julia/fundamental_types.html#Tuples-and-Named-Tuples) for a few examples.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [February 14, 2020, 8:14pm UTC](https://discourse.julialang.org/t/how-to-include-into-local-scope/34634/28 "2020-02-14T20:14:34Z")

</div>

> [@HenrikM](#):
>
> I will try to describe my use case without actually dumping the code here. I am building a plot that visualises 10 different things. The function would look like
> 
> ```julia
> function wowPlot(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, parameters, more_parameters)
> 
> ```

If you don’t have good defaults for these things, you can use [required kwargs](https://docs.julialang.org/en/v1/manual/functions/#Keyword-Arguments-1). Then you get the extra benefit of nice error-checking to ensure that all the variables are defined up front. And while a function with 10 positional arguments can certainly be difficult to use, there’s nothing wrong with many keyword arguments. The function call itself, then, becomes the place where you do all those assignments.

```julia
wowplot(
    x1=1:10,
    y1=rand(10),
    x2=1:10,
    y2=randn(10),
    width=3,
    height=4,
    style=:line,
    title="pretty plot",
    more="blah",
    andmore="blah",
    yetmore="blah")

```

[Previous page](https://discourse.julialang.org/t/how-to-include-into-local-scope/34634.md?page=1)
