# Coercing into a type

**URL:** <https://discourse.julialang.org/t/coercing-into-a-type/140299>\
**Category:** General Usage\
**Tags:** strings\
**Created:** [October 27, 2026, 10:49pm UTC](https://discourse.julialang.org/t/coercing-into-a-type/140299 "2026-10-27T22:49:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![freeman](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@freeman](https://discourse.julialang.org/u/freeman)\
**Post date:** [October 27, 2026, 10:49pm UTC](https://discourse.julialang.org/t/coercing-into-a-type/140299/1 "2026-10-27T22:49:30Z")

</div>

Sorry but I don’t know a better term than coercing.

Suppose that I read a set of strings and some times all of those strings can all be converted to InlineStrings.String15, which I prefer for performance, and some times not.

What is the cleanest way to do this?

Here’s one that I can think of, but I don’t like because I hate exceptions:

```julia-auto
s1 = ["kdjhfgksjdhfgkljsdhf", "lkjsdflskdflskdjflkfljs", "ldkfglkdfjglkdjfglkd"]
s2 = ["dfg", "dfg", "hkfj"]

maybeconvert(ss) = try
    InlineStrings.String15.(ss)
catch
    ss
end

maybeconvert(s1) # Vector{String}
maybeconvert(s2) # Vector{String15}

```

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [October 28, 2026, 9:07am UTC](https://discourse.julialang.org/t/coercing-into-a-type/140299/2 "2026-10-28T09:07:27Z")

</div>

`String15` can store up to 15 codeunits. So you can check if `ncodeunits` of each string is 15 or fewer:

```julia
function maybe_convert_string15(v::AbstractVector{<:Union{String, SubString{String}}}
    all(s -> ncodeunits(s) < 16, v) ? InlineStrings.String15.(v) : v
end

```

Your method also works, but checking the codeunits is more explicit about which error case you check for (i.e. you don’t accidentally catch some other exception).
