# Enum not at top level expression

**URL:** <https://discourse.julialang.org/t/enum-not-at-top-level-expression/23119>\
**Category:** New to Julia\
**Tags:** enum\
**Created:** [April 13, 2019, 7:34pm UTC](https://discourse.julialang.org/t/enum-not-at-top-level-expression/23119 "2019-04-13T19:34:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 13, 2019, 7:34pm UTC](https://discourse.julialang.org/t/enum-not-at-top-level-expression/23119/1 "2019-04-13T19:34:43Z")

</div>

Hello guys!

I have something like this:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/4/0449e5158dc85f305941ad2c578bc4ab480d453e.png)

So basically an enum array, where a specific enum like “Points” refers to a string and type “POINTS” / Float32. My question is:

If I try putting this in the main function I get a not at toplevel error, so is it in any way possible to insert this inside a function through some work around? I want it to be not assigned in the global scope.

Kind regards

---

<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:** [April 14, 2019, 5:41am UTC](https://discourse.julialang.org/t/enum-not-at-top-level-expression/23119/2 "2019-04-14T05:41:13Z")

</div>

Please don’t post screenshots, but quoted code.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![benibilme](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@benibilme](https://discourse.julialang.org/u/benibilme)\
**Post date:** [November 27, 2021, 12:17am UTC](https://discourse.julialang.org/t/enum-not-at-top-level-expression/23119/3 "2021-11-27T00:17:29Z")

</div>

Do you have solution? I have the same problem.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [November 27, 2021, 1:16am UTC](https://discourse.julialang.org/t/enum-not-at-top-level-expression/23119/4 "2021-11-27T01:16:38Z")

</div>

Is the problem having the enumeration at top level instead of inside a function? I do not believe it is possible to put an enumeration at function level, the enumeration defines a type, no? And types must be defined at top level.

---

<div class="post-metadata">

**Author:** ![benibilme](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@benibilme](https://discourse.julialang.org/u/benibilme)\
**Post date:** [December 1, 2021, 12:35am UTC](https://discourse.julialang.org/t/enum-not-at-top-level-expression/23119/5 "2021-12-01T00:35:17Z")

</div>

Yes. I am newbie and I find it weird not to be able to define enumaration in function. I have also noticed that I can not create constants as well.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [December 1, 2021, 5:30pm UTC](https://discourse.julialang.org/t/enum-not-at-top-level-expression/23119/6 "2021-12-01T17:30:52Z")

</div>

Yes, in Julia, does not make any sense to have any of these two things _inside local/function scope_ (both work in global scope).

Enumeration are types, and types are defined at the top level. What you expect by defining the enumeration inside the function? You expect the enumeration to only work inside the function, or that after calling your function the first time it becomes available for any function to use? Or you did it because you want to create an enumeration type on the fly, based on function parameters?

Constants are available at global level but not function level for a distinct reason: the compiler does not actually benefits from local/function scope constants (this does not make the code go faster or anything). You can, alternatively, force a local variable to only accept some type: `i :: Int = 5` guarantees that `i` cannot change the type to another type, but the value can be changed to other `Int` values. Also note that `const` just mean that you cannot assign a new object to that variable, but if your variable is mutable (like a `Vector{Int}`) you can have a global `const`ant you can call `push!`, `delete!` or anything that changes the object that already exists.

---

<div class="post-metadata">

**Author:** ![benibilme](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@benibilme](https://discourse.julialang.org/u/benibilme)\
**Post date:** [December 1, 2021, 9:32pm UTC](https://discourse.julialang.org/t/enum-not-at-top-level-expression/23119/7 "2021-12-01T21:32:00Z")

</div>

I am software engineer not datascientist. I am learning julia not for profession but for fun and as a tool for academic research. I have written too much C code, I guess. I am very accustomed using enumeration as array indexes. I am also accustomed to using constants, where ever possible, if I am not planning to modify the variable.

I am learning julia since I believe the motto, there is more than one way to do it, it seemed julia supported this paradigm. I ditched python because of that. I do not like being forced certain programming style.

Thank you for the response. Still, I do not like these decisions whatever the reasoning. It breaks “least unexpected behavior” for me, similar to crazy scoping rules, hard soft etc. I possibly programmed in 20 languges at least 10 of them could be stated dead. I have not seen such weird scoping rules yet. Starting from apl, fortran, smalltask, you name it. There are things that I do not like in julia, these could be counted as some of them.

Again thank you.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [December 2, 2021, 1:03am UTC](https://discourse.julialang.org/t/enum-not-at-top-level-expression/23119/8 "2021-12-02T01:03:57Z")

</div>

> [@benibilme](#):
>
> there is more than one way to do it

uhmm, I believe this is a [Perl language motto](https://en.wikipedia.org/wiki/There%27s_more_than_one_way_to_do_it). I am not sure Julia design goes on that direction, where did you find this association? Such motto is also often not seen as that much positive: [https://www.quora.com/Whats-the-one-programming-language-you-refuse-to-use-and-why](https://www.quora.com/Whats-the-one-programming-language-you-refuse-to-use-and-why)

> I am very accustomed using enumeration as array indexes.

This can be done, with a small conversion before:

```julia
julia> @enum Fruit apple=1 orange=2 kiwi=3

julia> ['a', 'b', 'c'][Int(orange)]
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)

```

> have not seen such weird scoping rules yet.

Not being able to define new types inside function is exceedingly common, `C` does not allow it, for example. The “no const in local scope” is more of a matter of implementation, nobody ever implemented this feature as it is not really useful (I think it may be implemented in the future as a crutch for the programmer). The hard vs. soft scoping rules were discussed to exhaustion through the years (you will find a lot of discussions in this forum). It is just a way to make the life of a beginner in the REPL easier, for anyone coding in a script/package it is irrelevant.
