String(array) destroys the array Bug or Feature?

a = Array{UInt8,1}(['a','b','c'])
length(a)     # 3
s = String(a) # "abc"
length(a)     # 0 Bug or feature ?

Same behaviour with julia-1.0.5, -1.2, -1.4.0-DEV.152

It’s documented in the help text for String, so it’s a feature:

help?> String
search: String string StringIndexError Cstring Cwstring bitstring randstring SubString include_string setrounding unsafe_string AbstractString escape_string


  Create a new String object from a byte vector v containing UTF-8 encoded characters. If v is Vector{UInt8} it will be truncated to zero length and future
  modification of v cannot affect the contents of the resulting string. To avoid truncation use String(copy(v)).

  When possible, the memory of v will be used without copying when the String object is created. This is guaranteed to be the case for byte vectors returned by
  take! on a writable IOBuffer and by calls to read(io, nb). This allows zero-copy conversion of I/O data to strings. In other cases, Vector{UInt8} data may be
  copied, but v is truncated anyway to guarantee consistent behavior.
1 Like

Thanks a lot! It is strange to have something changing the argument without the ! suffix. Isn’t it ?

Yeah, agreed. It may just be a historical artifact.


1 Like