gpt4 book ai didi

syntax-error - 为什么我不能在命名元组中将名称的类型指定为 Julia 中的符号元组?

转载 作者:行者123 更新时间:2023-12-03 07:58:57 24 4
gpt4 key购买 nike

有这个功能:

function Γ(state::Dict{Symbol, <:Tuple{Vararg{Symbol}}})::Vector{NamedTuple{<:Tuple{Vararg{Symbol}}, <:Tuple{Vararg{Symbol}}}}
kwargs = Dict(kwargs)
keys = [key for (key, value) ∈ state]
instances_vec = [value for (key, value) ∈ state]
data = Vector{Vector{Symbol}}()
function recursive(data, current, depth = 1)
if depth <= length(instances_vec)
for instance ∈ instances_vec[depth]
recursive(data, push!(copy(current), instance), depth + 1)
end
else
push!(data, current)
end
end
recursive(data, [])
return [(;zip(keys,configuration)...) for configuration ∈ data]
end

当像这样运行时:
identifiers = Dict(:Id=>(:One, :Two, :Three, :Four, :Five), :Nationality=>(:Brit, :German), :Color=>(:Red, :Blue, :Green))

Γ(identifiers)

给出以下错误:
ERROR: MethodError: Cannot `convert` an object of type
NamedTuple{(:Id, :Nationality, :Color),Tuple{Symbol,Symbol,Symbol}} to an object of type
NamedTuple{#s589,#s588} where #s588<:Tuple{Vararg{Symbol,N} where N} where #s589<:Tuple{Vararg{Symbol,N} where N}

返回的类型为: Array{NamedTuple{(:Id, :Nationality, :Color),Tuple{Symbol,Symbol,Symbol}},1}(:Id, :Nationality, :Color) 的类型是 Tuple{Symbol,Symbol,Symbol}所以我不明白为什么在不需要转换时会出现转换错误,特别是因为:
typeof((:Id, :Nationality, :Color)) <: Tuple{Vararg{Symbol}} && Tuple{Symbol,Symbol,Symbol} <: Tuple{Vararg{Symbol}} = true

最佳答案

首先,转换来自这样一个事实,即您的函数声明
声明一个返回类型。如果您删除(或注释掉)此类型注释

function Γ(state::Dict{Symbol, <:Tuple{Vararg{Symbol}}}) #::Vector{NamedTuple{<:Tuple{Vararg{Symbol}}, <:Tuple{Vararg{Symbol}}}}
kwargs = Dict() # <- there was a typo here in your minimal example
keys = [key for (key, value) ∈ state]
instances_vec = [value for (key, value) ∈ state]
data = Vector{Vector{Symbol}}()
function recursive(data, current, depth = 1)
if depth <= length(instances_vec)
for instance ∈ instances_vec[depth]
recursive(data, push!(copy(current), instance), depth + 1)
end
else
push!(data, current)
end
end
recursive(data, [])
return [(;zip(keys,configuration)...) for configuration ∈ data]
end

那么一切都很好:
julia> identifiers = Dict(:Id=>(:One, :Two, :Three, :Four, :Five), :Nationality=>(:Brit, :German), :Color=>(:Red, :Blue, :Green))
Dict{Symbol,Tuple{Symbol,Symbol,Vararg{Symbol,N} where N}} with 3 entries:
:Id => (:One, :Two, :Three, :Four, :Five)
:Nationality => (:Brit, :German)
:Color => (:Red, :Blue, :Green)

julia> Γ(identifiers)
30-element Array{NamedTuple{(:Id, :Nationality, :Color),Tuple{Symbol,Symbol,Symbol}},1}:
(Id = :One, Nationality = :Brit, Color = :Red)
(Id = :One, Nationality = :Brit, Color = :Blue)
(Id = :One, Nationality = :Brit, Color = :Green)
[...]

请注意,在必要时键入函数/方法的参数被认为是一种很好的做法。然而,为返回值提供类型断言的代码在 Julia 中通常不那么惯用:编译器非常擅长确定返回类型是什么!

现在这个转换失败的原因是因为提供的元素类型
签名与返回的实际元素类型不一致
向量:
julia> elem = (Id = :One, Nationality = :Brit, Color = :Red)
(Id = :One, Nationality = :Brit, Color = :Red)

julia> typeof(elem)
NamedTuple{(:Id, :Nationality, :Color),Tuple{Symbol,Symbol,Symbol}}

julia> elem isa NamedTuple{<:Tuple{Vararg{Symbol}}, <:Tuple{Vararg{Symbol}}}
false

对于 NamedTuple{A,B}匹配 NamedTuple{<:Tuple{Vararg{Symbol}},
<:Tuple{Vararg{Symbol}}}
,以下两个条件都必须成立:
  • A <: Tuple{Vararg{Symbol}}
  • B <: Tuple{Vararg{Symbol}}

  • 在这里,我们有:
    A = (:Id, :Nationality, :Color)
    B = Tuple{Symbol,Symbol,Symbol}

    所以,就像你说的, B 的条件持有:
    julia> B <: Tuple{Vararg{Symbol}}
    true

    但不是 A 的条件:
    julia> A <: Tuple{Vararg{Symbol}}
    ERROR: TypeError: in <:, expected Type, got Tuple{Symbol,Symbol,Symbol}
    Stacktrace:
    [1] top-level scope at REPL[10]:1

    # A is a value of type Tuple{...}, not the type itself
    julia> typeof(A) <: Tuple{Vararg{Symbol}}
    true

    关于syntax-error - 为什么我不能在命名元组中将名称的类型指定为 Julia 中的符号元组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60935579/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com