为未更改的字符串摆脱Julia的“警告:重新定义常量"? [英] Get rid of Julia's `WARNING: redifining constant` for strings that are not changed?

查看:129
本文介绍了为未更改的字符串摆脱Julia的“警告:重新定义常量"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的julia代码中,我使用了一些常量.其中一些常量是字符串(它们用作标识符).我的问题是,每当运行julia脚本时,即使不更改常量,我总是会收到以下关于常量字符串的警告: : WARNING: redefining constant pot_type

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

为了说明我的问题,这是一个MWE:

To illustrate my problem, here is a MWE:

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

如果我两次运行此脚本,则会收到上述警告. 不仅如此,如果我在Julia控制台中两次键入const something = "somestring",也会发生同样的事情.我刚得到WARNING: redefining constant something.

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.

我知道这不会以任何方式影响我的代码,但是无论如何都可以删除此警告或对其进行修复?在我的实际代码中,每次我提交内容时它都会创建5行,并且该空间可用于显示先前提交的输出.

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.

编辑(使自己更清晰):问题是,即使我没有重新定义常数,也显示此警告消息,这意味着我赋予它相同的值.而且,据我所知,此问题仅存在于String,而不存在于Int64Float64类型.例如:如果我先写const b = 1.2,然后再写const b = 1.4,我将得到预期的警告消息.现在,如果我先写const b = 1.2然后写const b = 1.2(相同的值),则不会再次收到警告,这与预期的一样.但是,这不适用于字符串常量.即使定义相同的值,您也会收到警告.

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.

推荐答案

为补充和阐述@aireties的出色答案,Julia当前有三种情况,当您执行const x = a并随后再次执行const x = b时,相同的模块:

To complement and elaborate on @aireties' excellent answer, there are three cases currently in Julia when you do const x = a and later do const x = b again in the same module:

  1. 无警告:如果a === bab在亨利·贝克(Henry Baker)的EGAL [

  1. No warning: if a === b then a and b are programmatically indistinguishable in the sense of Henry Baker's EGAL [1, 2]: replacing a with b cannot affect the behavior of any program, so this redefinition of the constant x is a no-op and no warning is needed.

警告::如果a !== b而不是typeof(a) == typeof(b),则在当前版本的Julia中,生成的代码仍然有效,因为它仅取决于x的类型,该类型仍然保留值保持不变,但是由于旧值a和新值b在程序上是可区分的,因此更改x可能会影响程序的行为.

Warning: if a !== b but typeof(a) == typeof(b) then in the current version of Julia, the generated code remains valid because it only depends on the type of x, which remains unchanged, but the behavior of the program is potentially affected by the change to x since the old value a and the new value b are programmatically distinguishable.

错误::如果typeof(a) != typeof(b)重新分配x会使使用x的任何先前生成的代码的假设无效.因此,不允许进行此分配.

Error: if typeof(a) != typeof(b) the reassignment of x would invalidate the assumptions of any previously generated code using x. Therefore this assignment is not allowed.

===谓词按类型和内容比较不可变的值,因此1.5始终与1.5相同,因为无论您拥有多少该值的实例",都无法对其进行突变,因此无法对其进行区分以任何方式.另一方面,值1.5(Float64)和Float32(1.5)是可区分的,因为它们具有不同的类型,即使它们表示的是完全相同的数值.但是,对于可变值,===谓词通过 identity 来比较对象:除非它们是完全相同的对象,否则不将它们视为===.这是因为,即使它们以相等的值开头,您也可以通过编程来区分两个可变值,方法是更改​​其中一个,然后查看另一个是否更改.

The === predicate compares immutable values by type and content so 1.5 is always the same as 1.5 since no matter how many "instances" of this value you have, they cannot be mutated and therefore cannot be distinguished in any way. The values 1.5 (Float64) and Float32(1.5), on the other hand, are distinguishable since they have different types, even though they represent the exact same numerical value. For mutable values, however, the === predicate compares objects by identity: unless they are the exact same object, they are not considered to be ===. This is because you can programmatically distinguish two mutable values, even if they start out with equal values, by changing one of them and seeing if the other one changes or not.

在Julia中,字符串仅按约定是不可变的:String类型包装一个字节数组,该数组是可变的; String类型只是不公开任何修改那些字节的功能.您仍然可以-并在大多数程序中不建议这样做-到达并改变字符串的字节.这意味着相等的字符串值的两个不同的实例是不同的对象,因此不会彼此===.因此,当您再次执行const x = "foo"和随后的const x = "foo"时,您将为x分配两个不同的!==字符串对象,因此您将收到有关以下内容的警告:

In Julia, strings are immutable by convention only: the String type wraps an array of bytes, which is mutable; the String type just doesn't expose any functions which modify those bytes. You can still – and this is not advised in most programs – reach in and mutate the bytes of a string. This means that two different instances of equal string values are different objects and therefore not === to each other. Thus, when you do const x = "foo" and later const x = "foo" again, you are assigning two different !== string objects to x so you get the warning that you're asking about:

julia> const x = "foo"
"foo"

julia> const x = "foo"
WARNING: redefining constant x
"foo"

另一方面,如果将 same 字符串对象分配给x两次,则不会收到警告:

If, on the other hand, you assign the same string object to x twice, you get no warning:

## in a new session ##

julia> foo = "foo"
"foo"

julia> const x = foo
"foo"

julia> const x = foo
"foo"

将来,我们可能会更改String的实现,以使其实际上是不可变的,在这种情况下,使用相同类型的相等字符串值更新const绑定将不再产生警告,因为这两个值将彼此===.

In the future, we might change the implementation of String so that it is actually immutable, in which case updating a const binding with and equal string value of the same type would no longer produce a warning since those two values would be === to each other.

这篇关于为未更改的字符串摆脱Julia的“警告:重新定义常量"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆