问题,关于方程和类型


#1

在提问之前请确定你已经努力阅读了文档,并且尝试自己在互联网上搜索。

请尽可能提供你的demo代码或者GitHub的gist地址。

abstract type ForceField end
struct ThermoCompute2
    filename::String
    quantities::Vector{Union{AbstractString,ForceField}}
    period::Int64
    overwrite::Bool
end

function init_thermocompute(;filename::String="test",quantities::Vector{Union{AbstractString,ForceField}}=["test"],period::Int64=1,overwrite::Bool=true)
    return ThermoCompute2(filename,quantities,period,overwrite)
end

compute_thermo=init_thermocompute(filename="test.thermo",quantities=["volume"],period=1,overwrite=true)

得到的错误是:
TypeError: in #init_thermocompute, in typeassert, expected Array{Union{AbstractString, ForceField},1}, got Array{String,1}

Stacktrace:
[1] (::getfield(Main, Symbol("#kw##init_thermocompute")))(::NamedTuple{(:filename, :quantities, :period, :overwrite),Tuple{String,Array{String,1},Int64,Bool}}, ::typeof(init_thermocompute)) at ./none:0
[2] top-level scope at In[39]:12


#2

应该是非常基本的错误但是看了一下没有发现,希望大神能指点一下。


#3

这里 quantities 的类型不对

julia> ["a"] isa Vector{<:AbstractString}
true

julia> ["a"] isa Vector{AbstractString}
false

可以再看下文档里有关类型部分的介绍:

https://docs.juliacn.com/latest/manual/types/#参数复合类型-1


#4

接楼上

julia> Vector{AbstractString}
Array{AbstractString,1}

julia> Vector{<:AbstractString}
Array{#s1,1} where #s1<:AbstractString

julia> Vector{T} where T <:AbstractString
Array{T,1} where T<:AbstractString

julia> ["a"] isa Vector{T} where T <:AbstractString
true

二三是等价的,第三种写法更清楚一点