data = Vector{UInt8}("hello, world!")
@assert(!isempty(data))
String(data)
@assert(!isempty(data)) # boom!
is this expected?
I found this issue: https://github.com/JuliaLang/julia/issues/24388, which eventually was resolved by copying data when converting string to a vector. Perhaps, when constructing a string from a vector, it should also copy?
I agree this is surprising, but it is explicitly documented:
help?> String
search: String string StringIndexError Cstring Cwstring bitstring SubString include_string setrounding unsafe_string AbstractString escape_string unescape_string AbstractUnitRange SubstitutionString setprecision AbstractIrrational
String(v::AbstractVector{UInt8})
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 of Vector{UInt8} data, use String(copy(v)); for other AbstractVector types, String(v) already makes a copy.
Hi. I am one of those people =) I agree that it should follow the expected behaviour and, if otherwise, better be called String!. I don’t think it matters much that it is documented, since the level of perplexity of such behaviour is so large. Nobody would check the constructor of strings before anything failing.