# "type definition not allowed inside a local scope"

**URL:** <https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 24, 2017, 12:13pm UTC](https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907 "2017-05-24T12:13:11Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dandan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dandan/32/8935_2.png) [@Dandan](https://discourse.julialang.org/u/Dandan)\
**Post date:** [May 24, 2017, 12:13pm UTC](https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907/1 "2017-05-24T12:13:11Z")

</div>

Hi.

Julia tells me that “type definition not allowed inside a local scope”.

I have a simple question - why? 🙂

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [May 24, 2017, 12:18pm UTC](https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907/2 "2017-05-24T12:18:30Z")

</div>

What are you doing when you get that error?

---

<div class="post-metadata">

**Author:** ![Dandan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dandan/32/8935_2.png) [@Dandan](https://discourse.julialang.org/u/Dandan)\
**Post date:** [May 24, 2017, 12:38pm UTC](https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907/3 "2017-05-24T12:38:32Z")

</div>

I just try to define type in a function scope 🙂

```julia
function localscope()
    type Foo
         baz::Int
         qux::Float64
    end
end

localscope()

```

I found this comment that _there are no “local types”_. I wonder why.  
[https://github.com/JuliaLang/julia/issues/2586#issuecomment-15018060](https://github.com/JuliaLang/julia/issues/2586#issuecomment-15018060)

---

<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:** [May 24, 2017, 1:07pm UTC](https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907/4 "2017-05-24T13:07:36Z")

</div>

> [@Dandan](#):
>
> I found this comment that there are no “local types”. I wonder why.

Part of the language design. Perhaps if you described your problem people would suggest solutions that work around this limitation.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 24, 2017, 1:16pm UTC](https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907/5 "2017-05-24T13:16:20Z")

</div>

It’s not exactly a limitation. Firstly, it’s not clear why you would need a type definition to be local to a function. Secondly, if you need a type definition to be local to a particular scope, you can make a new module, put it there and not export it.

---

<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:** [May 24, 2017, 1:31pm UTC](https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907/6 "2017-05-24T13:31:10Z")

</div>

Since people run into it sometimes, I would call it a limitation, but I agree that it is not a significant one in practice. I am curious about the use case of @Dandan though.

---

<div class="post-metadata">

**Author:** ![Dandan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dandan/32/8935_2.png) [@Dandan](https://discourse.julialang.org/u/Dandan)\
**Post date:** [May 24, 2017, 5:17pm UTC](https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907/7 "2017-05-24T17:17:23Z")

</div>

The motivation for question was this reasoning:  
_Why would I need type definition in global scope if I use it only in local scope?_  
_What if I want to declare and use type in local scope but with some name that already exists in global scope?_

It was not motivated by practice and it’s not a real limitation to me. Just curiosity)  
Maybe there are some arguments against this feature?

---

<div class="post-metadata">

**Author:** ![Dandan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dandan/32/8935_2.png) [@Dandan](https://discourse.julialang.org/u/Dandan)\
**Post date:** [May 24, 2017, 5:24pm UTC](https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907/8 "2017-05-24T17:24:06Z")

</div>

There is also another question. Assuming there are only global types - why we can’t define type with some code written within a function?

---

<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:** [May 24, 2017, 5:48pm UTC](https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907/9 "2017-05-24T17:48:45Z")

</div>

> [@Dandan](#):
>
> Why would I need type definition in global scope if I use it only in local scope?

Why would you want a type definition that you only use in local scope? Types are mainly useful to tie together information and functions passed between multiple scopes.

> [@Dandan](#):
>
> why we can’t define type with some code written within a function?

You can, using `eval`.

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [May 26, 2017, 5:53pm UTC](https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907/10 "2017-05-26T17:53:08Z")

</div>

The scoping rules are a bit awkward, but technically we could just allow this. We now already let you make local types by using closure syntax, so this wouldn’t be anything spectacularly different. Probably not a priority for 1.0, but could perhaps be a good up-for-grabs feature.

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [May 27, 2017, 3:32am UTC](https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907/11 "2017-05-27T03:32:54Z")

</div>

Can we get modules in local scope too while we’re at it?

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [May 27, 2017, 1:53pm UTC](https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907/12 "2017-05-27T13:53:26Z")

</div>

What’s the use case of allowing local modules? (just curious!)

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [May 27, 2017, 3:14pm UTC](https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907/13 "2017-05-27T15:14:47Z")

</div>

Currently, modules are only allowed at top level. Actually, for my purposes, broadening this to global scope would be enough. That’s so you can define modules inside macros. [https://github.com/JuliaLang/julia/issues/21009](https://github.com/JuliaLang/julia/issues/21009)

---

<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:** [May 27, 2017, 3:42pm UTC](https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907/14 "2017-05-27T15:42:22Z")

</div>

It would be interesting to see a use case for macros that define modules.

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [May 27, 2017, 3:48pm UTC](https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907/15 "2017-05-27T15:48:25Z")

</div>

I’m interested so that you can chain all the code in a module. But yeah I bet there are some pretty cool applications.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [May 27, 2017, 4:06pm UTC](https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907/16 "2017-05-27T16:06:41Z")

</div>

I suppose one use case would be for an enum like macro, that defined a baremodule with a set of constant values of different types.  
I’ve done that manually a number of times, to avoid polluting a namespace, and to have easier to use names for the constants, i.e. `Foo.BAR`, `Foo.BAZ` instead of `FOO_BAR`, `FOO_BAZ`, I only need to export the module name `Foo`.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [May 27, 2017, 9:45pm UTC](https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907/17 "2017-05-27T21:45:33Z")

</div>

> [@bramtayl](#):
>
> That’s so you can define modules inside macros.

```julia
macro macmod(block)
       return Expr(:toplevel, esc(block))
end

@macmod module Foo
       foo(x) = x^3
end

```

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [May 27, 2017, 9:53pm UTC](https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907/18 "2017-05-27T21:53:27Z")

</div>

Yes, this has been pointed out to me. But according to Jeff (see the linked issue), “it’s not generally correct to just replace block with toplevel, or to move code around”

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [May 31, 2017, 5:51am UTC](https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907/19 "2017-05-31T05:51:17Z")

</div>

The main downside is that it means your macro can only be used at the toplevel. In many cases, that simply means it would be clearer to write the `eval` explicitly.

---

<div class="post-metadata">

**Author:** ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)\
**Post date:** [July 4, 2019, 4:12pm UTC](https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907/20 "2019-07-04T16:12:40Z")

</div>

I just got this error and found the old thread.

In my use case, I’m defining new types within a `@testset` block. This makes sense to me, because I might want to test many cases, without wanting to deal with name conflicts etc.

Anyway, the [tests](https://travis-ci.org/SCIP-Interfaces/SCIP.jl/builds/554315684) are passing with Julia 1.1 but I get the error message in Julia 1.0, so I guess this behavior was changed?

[Next page](https://discourse.julialang.org/t/type-definition-not-allowed-inside-a-local-scope/3907.md?page=2)
