Enum not at top level expression

Hello guys!

I have something like this:

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

Please don’t post screenshots, but quoted code.

3 Likes

Do you have solution? I have the same problem.

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.

1 Like

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.

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 constant you can call push!, delete! or anything that changes the object that already exists.

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.

uhmm, I believe this is a Perl language motto. 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

I am very accustomed using enumeration as array indexes.

This can be done, with a small conversion before:

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.

3 Likes