Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get rid of Julia's `WARNING: redifining constant` for strings that are not changed?

In my julia code I am using some constants. Some of these constants are strings (they serve as identifiers). My issues is that whenever I run a julia script, I always get the following warning for constant strings, even when I do not change the constants : WARNING: redefining constant pot_type

To illustrate my problem, here is a MWE:

const pot_type = "constant"
const b = 12
println("Given parameters: Potential = $pot_type, b = $b .")

If I run this script two times, I will get the aforementioned warning. Not only that, but the same thing will happen if I just type const something = "somestring" two times in the Julia console. I just get WARNING: redefining constant something.

I am aware that this does not affect my code in any way, but is there anyway to remove this warning or to fix it? In my actual code it creates 5 lines every time I submit something and this space could be used to display the output of previous submissions.

EDIT (making myself clearer): The problem is that this WARNING message is displayed even when I am NOT redefining a constant, meaning that I give it the same value. And also, this problem (as far as I know) exists ONLY for String , not for Int64 or Float64 types. E.g.: if I write const b = 1.2 and then const b = 1.4 I will get the warning message as expected. Now, if I write const b = 1.2 and then const b = 1.2 (same value), I will NOT get the warning, again as expected. However this does not work with string constants. You will get the warning even when defining the same value.

like image 247
George Datseris Avatar asked Dec 07 '25 07:12

George Datseris


1 Answers

If you are going to use something as a constant then you really don't want to be defining it multiple times (after all, that's the point of a constant) and when you run your script multiple times, that's exactly what you are doing. Also, it's not quite true that redefining constants doesn't affect your code in any way - it actually can hurt performance quite a bit in some situations. Again, that's why you use constants - to improve performance by making explicit that the computer doesn't need to worry about particular objects having their values changed.

Even if you are assigning the constant the same value, it's still going to look like a new assignment to the computer. I.e. the computer won't, at least without you more explicitly telling it to (as I describe below) perform some additional logic to see "oh, I guess this is just the same value that is getting assigned to the constant that it had before, I suppose that's probably ok."

In general, situations where you are running the same script multiple times just arise in development, which I am guessing is what you're doing. Thus, as a quick fix to this, you could check to see if your constant is already defined and only assign it a value if it isn't. The following will accomplish that:

isdefined(:b) || (const b = 12)

As a longer term solution, once you get past the development stage (where you keep running the same bit of code again and again to get the bugs worked out) then you'll really want to write your script so that it doesn't get to defining a constant multiple times, since otherwise, as I mentioned, it's not really a constant in that case.

P.S. in case it's helpful, the interpretation of the above code snippet is as follows. The || operator means that Julia will evaluate the first statement isdefined(:b) and only go on to the second statement, (const b = 12) if the first is false. We use the : operator in the first statement to refer to the symbol for b, i.e. to ask is there anything assigned to that symbol, b.

like image 190
Michael Ohlrogge Avatar answered Dec 10 '25 06:12

Michael Ohlrogge