Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia append!() Cannot `convert` an object of type Char to an object of type String

Tags:

julia

I'm trying to add a String to an array, as follows:

arry = String[]
append!(arry, "test")

but I'm getting the following error:

Cannot `convert` an object of type Char to an object of type String

Why am I getting this error when I'm adding a String, not a Char?

(posting my own answer below for the benefit of other Julia newbies - anyone with more helpful tips please jump in!)

like image 399
J. Blauvelt Avatar asked Oct 25 '25 14:10

J. Blauvelt


1 Answers

I cannot see your answer, but here is a typical pattern.

You can push! a single element or append! a collection:

julia> arry = String[]
0-element Array{String,1}

julia> push!(arry, "test")
1-element Array{String,1}:
 "test"

julia> append!(arry, ("test",))
2-element Array{String,1}:
 "test"
 "test"

julia> append!(arry, ["test"])
3-element Array{String,1}:
 "test"
 "test"
 "test"

julia> append!(arry, Ref("test"))
4-element Array{String,1}:
 "test"
 "test"
 "test"
 "test"

Note that a collection can be e.g. a Tuple, a Vector or Ref (which is kind of 0-dimensional and non-allocating collection that is often encountered in broadcasting).

like image 101
Bogumił Kamiński Avatar answered Oct 28 '25 04:10

Bogumił Kamiński



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!