I am confusing with when constructing a 1-d array of the specified type by usung getindex(type[, elements...])
.
Of course, that I can convert Int 8 when the element are Int
getindex(Int8, 1, 2)
2-element Vector{Int8}:
1
2
Even when the element are character format, I can convert it to Int8 :
getindex(Int8, '1', '2')
2-element Vector{Int8}:
49
50
However, I can not convert when the element are in string format.
getindex(Int8, "1", "2")
and, raise the following error :
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
Why getindex()
allow character element to convert to different format (like character -> Int8), but string ?
First of all, that's a rather weird way of writing array literals: getindex(T, xs...)
is usually written as T[xs...]
.
However, the error already quite clearly tells you what went wrong:
Cannot
convert
an object of type String to an object of type Int8
How do you imagine a general conversion from String
to Int8
to look like? What 8-bit integer should correspond to the string "slkdfjls"
, for example? A string is after all a pretty much arbitrary sequence of bytes. And contrary to your expectation, Julia does not make an attempt to do any parsing of the contained value (for that, use parse(Int8, "1")
.
Characters on the other hand represent (if valid) single UTF-8 code points, and it is meaningful to reinterpret their fixed amount of bytes as a number:
julia> convert(Int16, '†')
8224
julia> convert(Int8, '1') # certainly not Int8(1)!
49
The conversion is already borderline meaningful when the value exceeds the range of the target type:
julia> convert(Int8, '†')
ERROR: InexactError: trunc(Int8, 8224)
...
UTF-8 characters that happen to be representable by only one byte can be losslessly converted to Int8
; this covers all of ASCII. Above that, an error is raised. This is no different from convert(Int8, Int32(something))
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With