为什么这个列表理解返回一个数组{Any,1}而不是一个数组{Symbol,1}?

时间:2021-10-23 20:46:15

When I try to create an Array with a list comprehension, it results in an Array{Any, 1} even if I encode all the elements to "symbol":

当我尝试使用列表推导创建一个数组时,即使我将所有元素编码为“符号”,它也会产生一个数组{Any,1}:

julia> u_col_names=[symbol("user_id"), symbol("age"), symbol("sex"), symbol("occupation"), symbol("zip_code")]
5-element Array{Symbol,1}:
 :user_id   
 :age       
 :sex       
 :occupation
 :zip_code 

julia> col_names=["user_id", "age", "sex", "occupation", "zip_code"]
5-element Array{ASCIIString,1}:
 "user_id"   
 "age"       
 "sex"       
 "occupation"
 "zip_code"  

julia> u_col_names=[symbol(col_names[i]) for i in 1:size(col_names)[1]]
5-element Array{Any,1}:
 :user_id   
 :age       
 :sex       
 :occupation
 :zip_code 

Why does that last list comprehension return an Array{Any, 1} instead of an Array{Symbol, 1}? Note that the following does return an Array{Symbol, 1}:

为什么最后一个列表解析返回一个数组{Any,1}而不是一个数组{Symbol,1}?请注意,以下操作会返回一个数组{Symbol,1}:

julia> u_col_names=[symbol("col_names$i") for i in 1:size(col_names)[1]]
5-element Array{Symbol,1}:
 :col_names1
 :col_names2
 :col_names3
 :col_names4
 :col_names5

Interestingly, so does the following:

有趣的是,以下内容也是如此:

julia> col_names[1]
"user_id"

julia> symbol(col_names[1])
:user_id

julia> [symbol(col_names[1]), symbol(col_names[2])]
2-element Array{Symbol,1}:
 :user_id
 :age    

What am I missing?

我错过了什么?

1 个解决方案

#1


8  

According to this discussion in the issue tracker of the JuliaLang/julia repo on GitHub, the problem seems to stem from a deficiency of Julia's type-inference system. Jeff Bezanson (one of the Julia writers and maintainers) left a relevant comment in another discussion:

根据GitHub上JuliaLang / julia回购的问题跟踪器中的讨论,问题似乎源于Julia的类型推理系统的缺陷。 Jeff Bezanson(朱莉娅作家和维护者之一)在另一场讨论中留下了相关评论:

This behavior is actually expected at the moment. Since [col_names] is global, it might change anywhere so we can't assume we know its type. This is overly pessimistic, but it's hard to come up with a rule that would let us do better.

目前实际上预计会出现此行为。由于[col_names]是全局的,它可能会在任何地方改变,所以我们不能假设我们知道它的类型。这是过于悲观,但很难提出一条让我们做得更好的规则。

Surprisingly, perhaps (as observed by John Myles White), the type is correctly inferred if those operations are carried out inside a function:

令人惊讶的是,也许(如John Myles White所观察到的),如果这些操作是在函数内部执行的,则正确推断出类型:

julia> function fun()
         col_names=["user_id", "age", "sex", "occupation", "zip_code"]
         return u_col_names=[symbol(item) for item in col_names]
       end
fun (generic function with 1 method)

julia> fun()
5-element Array{Symbol,1}:
 :user_id   
 :age       
 :sex       
 :occupation
 :zip_code

As an alternative to your list comprehension, you can use map(symbol, <Array{T,1}>), which does return an Array{Symbol,1}, even at the global scope:

作为列表推导的替代方法,您可以使用map(symbol, ),它确实返回一个数组{Symbol,1},即使在全局范围内:

julia> col_names=["user_id", "age", "sex", "occupation", "zip_code"]
5-element Array{ASCIIString,1}:
 "user_id"   
 "age"       
 "sex"       
 "occupation"
 "zip_code"  

julia> map(symbol, col_names)
5-element Array{Symbol,1}:
 :user_id   
 :age       
 :sex       
 :occupation
 :zip_code 

#1


8  

According to this discussion in the issue tracker of the JuliaLang/julia repo on GitHub, the problem seems to stem from a deficiency of Julia's type-inference system. Jeff Bezanson (one of the Julia writers and maintainers) left a relevant comment in another discussion:

根据GitHub上JuliaLang / julia回购的问题跟踪器中的讨论,问题似乎源于Julia的类型推理系统的缺陷。 Jeff Bezanson(朱莉娅作家和维护者之一)在另一场讨论中留下了相关评论:

This behavior is actually expected at the moment. Since [col_names] is global, it might change anywhere so we can't assume we know its type. This is overly pessimistic, but it's hard to come up with a rule that would let us do better.

目前实际上预计会出现此行为。由于[col_names]是全局的,它可能会在任何地方改变,所以我们不能假设我们知道它的类型。这是过于悲观,但很难提出一条让我们做得更好的规则。

Surprisingly, perhaps (as observed by John Myles White), the type is correctly inferred if those operations are carried out inside a function:

令人惊讶的是,也许(如John Myles White所观察到的),如果这些操作是在函数内部执行的,则正确推断出类型:

julia> function fun()
         col_names=["user_id", "age", "sex", "occupation", "zip_code"]
         return u_col_names=[symbol(item) for item in col_names]
       end
fun (generic function with 1 method)

julia> fun()
5-element Array{Symbol,1}:
 :user_id   
 :age       
 :sex       
 :occupation
 :zip_code

As an alternative to your list comprehension, you can use map(symbol, <Array{T,1}>), which does return an Array{Symbol,1}, even at the global scope:

作为列表推导的替代方法,您可以使用map(symbol, ),它确实返回一个数组{Symbol,1},即使在全局范围内:

julia> col_names=["user_id", "age", "sex", "occupation", "zip_code"]
5-element Array{ASCIIString,1}:
 "user_id"   
 "age"       
 "sex"       
 "occupation"
 "zip_code"  

julia> map(symbol, col_names)
5-element Array{Symbol,1}:
 :user_id   
 :age       
 :sex       
 :occupation
 :zip_code