String interpolation of numbers

This is sort of a SwiftUI question, but I think there's an underlying Swift question, so I'm starting here…

If I use a SwiftUI Text View with a literal label like this:


assuming the value of someLargeInt has lots of digits, I get a formatted string that uses , separators:


But if I return that same Swift string from a function or property:

    var someLargeIntAsText: String { "\(someLargeInt)" }

I get a formatted string without separators:


Is there some localization context that's assumed with the Text parameter that is different inside a function? Or is there something else going on, or is this a bug?

ExpressibleByStringInterpolation is where the segments ("", someLargeInt, "") are being redirected into some sort of formatter. But it is actually LocalizedStringKey that has that conformance and does the work. (The string literal is creating a LocalizedStringKey, not a String.)

If you do Text(verbatim: "\(someLargeInt)"), you’ll get 123456789 instead, because that initializer actually takes a String.

1 Like

That's a bit subtle. In effect, there are two init(_:) initializers, but they have different behavior. <sigh>

Since a literal string would satisfy the type requirement of both initializers, what determines which one is used? The one without the generic type?


That sounds like a truly terrible design choice (an invisible syntactic detail determining a semantic outcome), but I guess that's OT for this forum.