构造指定类型的一维数组时字符和字符串的区别
Difference between character and string when constructing a 1-d array of the specified type
我在使用 usung getindex(type[, elements...])
构造指定类型的一维数组时感到困惑。
当然,当元素是Int
时,我可以转换Int 8
getindex(Int8, 1, 2)
2-element Vector{Int8}:
1
2
即使元素是 character 格式,我也可以将其转换为 Int8 :
getindex(Int8, '1', '2')
2-element Vector{Int8}:
49
50
但是,当元素是 string 格式时,我无法转换。
getindex(Int8, "1", "2")
并且,引发以下错误:
MethodError: Cannot `convert` an object of type String to an object of type Int8
Closest candidates are:
convert(::Type{T}, ::Ptr) where T<:Integer at pointer.jl:23
convert(::Type{IT}, ::GeometryBasics.OffsetInteger) where IT<:Integer at C:\Users\Admin\.julia\packages\GeometryBasics\WMp6v\src\offsetintegers.jl:40
convert(::Type{T}, ::SentinelArrays.ChainedVectorIndex) where T<:Union{Signed, Unsigned} at C:\Users\CARVI\.julia\packages\SentinelArrays\tV9lH\src\chainedvector.jl:209
...
Stacktrace:
[1] setindex!(A::Vector{Int8}, x::String, i1::Int64)
@ Base .\array.jl:839
[2] getindex(#unused#::Type{Int8}, x::String, y::String)
@ Base .\array.jl:393
[3] top-level scope
@ In[35]:1
[4] eval
@ .\boot.jl:360 [inlined]
[5] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base .\loading.jl:1116
为什么 getindex()
允许 character 元素转换为不同的格式(如字符 -> Int8),但 string ?
首先,这是一种相当奇怪的数组字面量的写法:getindex(T, xs...)
通常写成 T[xs...]
.
但是,错误已经很清楚地告诉你出了什么问题:
Cannot convert
an object of type String to an object of type Int8
您如何想象从 String
到 Int8
的一般转换看起来像?例如,字符串 "slkdfjls"
应该对应什么 8 位整数?一个字符串毕竟是一个非常任意的字节序列。与您的预期相反,Julia 确实 没有 尝试对包含的值进行任何解析(为此,请使用 parse(Int8, "1")
.
另一方面,字符表示(如果有效)单个 UTF-8 代码点,将它们的固定字节数重新解释为数字是有意义的:
julia> convert(Int16, '†')
8224
julia> convert(Int8, '1') # certainly not Int8(1)!
49
当值超出目标类型的范围时,转换已经具有边界意义:
julia> convert(Int8, '†')
ERROR: InexactError: trunc(Int8, 8224)
...
碰巧只能用一个字节表示的UTF-8字符可以无损转换为Int8
;这涵盖了所有的 ASCII。在此之上,会引发错误。这与 convert(Int8, Int32(something))
.
没有区别
我在使用 usung getindex(type[, elements...])
构造指定类型的一维数组时感到困惑。
当然,当元素是Int
时,我可以转换Int 8getindex(Int8, 1, 2)
2-element Vector{Int8}:
1
2
即使元素是 character 格式,我也可以将其转换为 Int8 :
getindex(Int8, '1', '2')
2-element Vector{Int8}:
49
50
但是,当元素是 string 格式时,我无法转换。
getindex(Int8, "1", "2")
并且,引发以下错误:
MethodError: Cannot `convert` an object of type String to an object of type Int8
Closest candidates are:
convert(::Type{T}, ::Ptr) where T<:Integer at pointer.jl:23
convert(::Type{IT}, ::GeometryBasics.OffsetInteger) where IT<:Integer at C:\Users\Admin\.julia\packages\GeometryBasics\WMp6v\src\offsetintegers.jl:40
convert(::Type{T}, ::SentinelArrays.ChainedVectorIndex) where T<:Union{Signed, Unsigned} at C:\Users\CARVI\.julia\packages\SentinelArrays\tV9lH\src\chainedvector.jl:209
...
Stacktrace:
[1] setindex!(A::Vector{Int8}, x::String, i1::Int64)
@ Base .\array.jl:839
[2] getindex(#unused#::Type{Int8}, x::String, y::String)
@ Base .\array.jl:393
[3] top-level scope
@ In[35]:1
[4] eval
@ .\boot.jl:360 [inlined]
[5] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base .\loading.jl:1116
为什么 getindex()
允许 character 元素转换为不同的格式(如字符 -> Int8),但 string ?
首先,这是一种相当奇怪的数组字面量的写法:getindex(T, xs...)
通常写成 T[xs...]
.
但是,错误已经很清楚地告诉你出了什么问题:
Cannot
convert
an object of type String to an object of type Int8
您如何想象从 String
到 Int8
的一般转换看起来像?例如,字符串 "slkdfjls"
应该对应什么 8 位整数?一个字符串毕竟是一个非常任意的字节序列。与您的预期相反,Julia 确实 没有 尝试对包含的值进行任何解析(为此,请使用 parse(Int8, "1")
.
另一方面,字符表示(如果有效)单个 UTF-8 代码点,将它们的固定字节数重新解释为数字是有意义的:
julia> convert(Int16, '†')
8224
julia> convert(Int8, '1') # certainly not Int8(1)!
49
当值超出目标类型的范围时,转换已经具有边界意义:
julia> convert(Int8, '†')
ERROR: InexactError: trunc(Int8, 8224)
...
碰巧只能用一个字节表示的UTF-8字符可以无损转换为Int8
;这涵盖了所有的 ASCII。在此之上,会引发错误。这与 convert(Int8, Int32(something))
.