# Why are concatenated String15's promoted to String31?

**URL:** https://discourse.julialang.org/t/why-are-concatenated-string15s-promoted-to-string31/124686
**Category:** General Usage
**Tags:** strings, accessors
**Created:** [January 11, 2025, 11:03pm UTC](https://discourse.julialang.org/t/why-are-concatenated-string15s-promoted-to-string31/124686 "2025-01-11T23:03:51Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Tetrakai](https://avatars.discourse-cdn.com/v4/letter/t/4da419/32.png) [@Tetrakai](https://discourse.julialang.org/u/Tetrakai)
#### Post date: [January 11, 2025, 11:03pm UTC](https://discourse.julialang.org/t/why-are-concatenated-string15s-promoted-to-string31/124686/1 "2025-01-11T23:03:51Z")

</div>

I found this bug (I guess it is mine) when trying to `@reset` an element of an `SVector{String15}` after updating `Accessors` from [0.1.38 to 0.1.39](https://github.com/JuliaObjects/Accessors.jl/compare/v0.1.38...v0.1.39), but not sure why it used to work.

MWE:

```julia
julia> using InlineStrings

julia> x = String15("A")
"A"

julia> y = x*String15("B")
"AB"

julia> typeof(y)
String31

```

It was fixed by wrapping the result in another `String15()`, but it seems like the default should be to keep the same type.

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [January 11, 2025, 11:45pm UTC](https://discourse.julialang.org/t/why-are-concatenated-string15s-promoted-to-string31/124686/2 "2025-01-11T23:45:44Z")

</div>

The concatenation of two length-15 strings produce a length-30 string, which can’t be represented by a `String15`. To ensure type-stability, the result of concatenating `String15`s must not depend on their length.

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [January 12, 2025, 12:27am UTC](https://discourse.julialang.org/t/why-are-concatenated-string15s-promoted-to-string31/124686/3 "2025-01-12T00:27:11Z")

</div>

Using an internal function of InlineStrings `addcodeunit` a workaround can be managed:

```julia
appendstring(x,y) = 
  foldl(codeunits(y); init=(x,false)) do (a,enough),b
      enough ? (a,enough) : InlineStrings.addcodeunit(a,b)
  end |> first

```

with this function:

```julia
julia> x = String15("A");

julia> y = appendstring(x,String15("B"))
"AB"

julia> typeof(y)
String15

```

WARNING: This uses an internal function which is not a `public` interface of InlineStrings.
