# "\`const static\`" function parameters

**URL:** <https://discourse.julialang.org/t/const-static-function-parameters/1803>\
**Category:** General Usage\
**Created:** [January 31, 2017, 4:54pm UTC](https://discourse.julialang.org/t/const-static-function-parameters/1803 "2017-01-31T16:54:10Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [November 9, 2017, 4:14pm UTC](https://discourse.julialang.org/t/const-static-function-parameters/1803/21 "2017-11-09T16:14:52Z")

</div>

Does this work?

```julia
myfun = let
       const x = rand(2,2)
       function myfun()
       x
       end
       end
myfun()

```

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [November 9, 2017, 4:22pm UTC](https://discourse.julialang.org/t/const-static-function-parameters/1803/22 "2017-11-09T16:22:19Z")

</div>

How would you pass arguments with this syntax?

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [November 9, 2017, 4:34pm UTC](https://discourse.julialang.org/t/const-static-function-parameters/1803/23 "2017-11-09T16:34:29Z")

</div>

```julia
julia> myfun = let
       const x = rand(2,2)
       function myfun(y)
       x+y
       end
       end
myfun (generic function with 2 methods)

julia> myfun(5)
2×2 Array{Float64,2}:
 5.09896 5.08873
 5.34033 5.04235

```

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [November 9, 2017, 4:45pm UTC](https://discourse.julialang.org/t/const-static-function-parameters/1803/24 "2017-11-09T16:45:10Z")

</div>

Yep, that is very nice. It seems to have exactly the behavior I want. The code looks a bit ugly, but it gets the job done. If one could accomplish this with a macro that would be nice. I have a very limited understanding of macros, so it is not 100% clear to me if the macros suggested above accomplish this.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 9, 2017, 4:49pm UTC](https://discourse.julialang.org/t/const-static-function-parameters/1803/25 "2017-11-09T16:49:18Z")

</div>

Aside, I don’t think the `const` is needed as `x` is in a local scope and not global.

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [November 9, 2017, 4:58pm UTC](https://discourse.julialang.org/t/const-static-function-parameters/1803/26 "2017-11-09T16:58:17Z")

</div>

This is probably nicer, I’m not sure if the const is neccesary here.

```julia
julia> module HidingModule
       export myfun
       const x = rand(2,2)
       myfun(y) = x+y
       end
HidingModule

julia> using HidingModule

julia> myfun(6)
2×2 Array{Float64,2}:
 6.74679 6.51266
 6.33858 6.27355

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 9, 2017, 5:08pm UTC](https://discourse.julialang.org/t/const-static-function-parameters/1803/27 "2017-11-09T17:08:47Z")

</div>

Yes, global is needed then as x is in the global scope of the HidingModule. I’d say the most idiomatic is

```julia
let
    x = rand(2,2)
    global function myfun(y)
       x+y
    end
end

```

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [November 9, 2017, 5:09pm UTC](https://discourse.julialang.org/t/const-static-function-parameters/1803/28 "2017-11-09T17:09:02Z")

</div>

There is another issue tracking this.  
[https://github.com/JuliaLang/julia/issues/15056](https://github.com/JuliaLang/julia/issues/15056)

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [November 9, 2017, 5:35pm UTC](https://discourse.julialang.org/t/const-static-function-parameters/1803/29 "2017-11-09T17:35:18Z")

</div>

The thing I don’t like about this is that it messes with the natural identation of the code. Functions are supposed to be without identation, but the let block forces you to indent (otherwise you could look at the `x = rand(2,2)` and not realize what it is doing out there). The only downside is readability.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 9, 2017, 5:50pm UTC](https://discourse.julialang.org/t/const-static-function-parameters/1803/30 "2017-11-09T17:50:44Z")

</div>

> [@e3c6](#):
>
> Functions are supposed to be without identation, but the let block forces you to indent

Nope. Functions should be indented like everything else at the same level, eg for a closure

```julia
function add_this(x)
    function(y)
        x+y
    end
end

```

The indentations above are correct: they are inside a `let` block, and everything else at the same level would be indented by the same amount.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [November 9, 2017, 5:57pm UTC](https://discourse.julialang.org/t/const-static-function-parameters/1803/31 "2017-11-09T17:57:33Z")

</div>

I meant global functions. In your example, the closure is not callable from the outside.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 9, 2017, 6:03pm UTC](https://discourse.julialang.org/t/const-static-function-parameters/1803/32 "2017-11-09T18:03:20Z")

</div>

Most languages that support closures like @mauro3’s example indent like this, eg

```nohighlight
(let ((x (random 100)))
  (defun myfun (y)
    (+ x y)))

```

The issue is not whether the function is in the global namespace, but that `let` starts a block.

[Previous page](https://discourse.julialang.org/t/const-static-function-parameters/1803.md?page=1)
