SE-0559: Add `bidiClass` to `Unicode.Scalar.Properties`

Hello, Swift community!

The review of SE-0559: Add bidiClass to Unicode.Scalar.Properties begins now and runs through November 4, 2026.

Reviews are an important part of the Swift evolution process. All review feedback should be either on this forum thread or, if you would like to keep your feedback private, directly to me as the review manager by email or DM. When contacting the review manager directly, please put "SE-0559" in the subject line.

What goes into a review?

The goal of the review process is to improve the proposal under review through constructive criticism and, eventually, determine the direction of Swift. When writing your review, here are some questions you might want to answer in your review:

  • What is your evaluation of the proposal?
  • Is the problem being addressed significant enough to warrant a change to Swift?
  • Does this proposal fit well with the feel and direction of Swift?
  • If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?
  • How much effort did you put into your review? A glance, a quick reading, or an in-depth study?

More information about the Swift evolution process is available at:

swift-evolution/process.md at main · swiftlang/swift-evolution · GitHub

Thank you,

Doug Gregor
Review Manager

7 Likes

Makes sense to me. I'll bikeshed bidirectionalCharacterClass or some such since "bidi", while a Unicode term of art, is maybe not widely known outside us grapheme cluster aficionados. But I don't feel strongly, and I see obvious use cases for the property, so :ship:.

4 Likes

Unless I'm mistaken, the proposal here is choosing to expose rawValue publicly. There's a tradeoff for that (in that it's not meaningful) but an understandable choice.

But, given that choice, I believe a @c @objc enum that uses UInt8 raw values would satisfy all of these problems. Has that been explored? The type would be nonfrozen but occupy a single byte, be passed and returned directly across library evolution boundaries, support nesting inside the Unicode namespace, and work without Obj-C interop.

+1

Will these be removable by the linker for builds on platforms where you statically link all your Swift libraries? EG will this increase the size of "hello world" on android?

And is there any hope of getting East Asian Width tables? I'd like to use it in swift-syntax to handle non latin scripts more correctly (right now we pretend nothing is wide), but I'm aware there is more complexity with this table, and we could codegen efficient tables for that use anyway.

+1. Foundation runs into the same problem with other scalar properties. .caseInsensitive and .diacriticInsensitive need case folding and canonical decomposition, and in package builds on Darwin the _Unicode SPI that provides them isn't visible. So Support case, diacritic and width options outside the framework by niktishakov · Pull Request #2349 · swiftlang/swift-foundation · GitHub adds its own tables (about 7 KB), with a TODO pointing to Add all of the Unicode properties · Issue #58697 · swiftlang/swift · GitHub. Public APIs for those properties, like bidiClass here, would let Foundation drop them.

1 Like

Like Unicode.CanonicalCombiningClass, Unicode.BidiClass is a RawRepresentable struct with a UInt8 raw value and one static member per Bidi_Class value.

I think the raw integer values of Unicode.BidiClass should be private or internal, because they aren't defined by the Unicode Standard.

Alternatively, the raw values could be the short identifiers ("L", "R", "AL", "NSM", etc.) This would be similar to the design of Foundation::Locale.Script and Foundation::Locale.Region — which also have conformances to Codable, CustomDebugStringConvertible, and ExpressibleByStringLiteral. (Would CaseIterable be useful?)

import Foundation

print(Foundation::Locale.Script.unknown)  // prints "Zzzz"
print(Foundation::Locale.Region.unknown)  // prints "ZZ"
2 Likes

I think that's a perfectly reasonable alternative name.

It's needed for the RawRepresentable conformance. We don't have anything to match in-memory representations with, so it's not adding as much signaling value to this type as for other types that follow this pattern. But, given that it's frozen, I don't feel that we should go out of our way to hide the raw value.

It would land us in a similar position. From an aesthetic perspective, it feels a little off as this has nothing to do with Objective-C. Practically speaking, I think it would add a synthesized trapping default case in switches with no explicit default. I'm not sure whether that's valuable or not.

Understanding what effect these annotations have in practice ends up being a small research project every time. Whatever the behavior actually is, @objc communicates our intent less directly than @frozen. I am open to it, but would like some more guidance from the LSG on using these attributes for "off label" purposes.

It is unfortunate that @c has the wrong semantics, but @objc does exactly what we want representationally, gets us better diagnostics for situations like duplicated values, and reduces the need for optimizer heroics in switch statements.

Even though this is an "off-label" use, @objc is no more misleading than using struct when we are clearly dealing with something that is conceptually an enum. We should use @objc here.

3 Likes

+1 to this. I don't think Swift should add another encoding of these values, but the short names are potentially useful (e.g. this is how they would be encoded in a Regex, so if you were going to transmit them to another system for some reason or encode them in a JSON file, that's a good value to use).

Other than that:

  • I think bidiClass is the correct name for this property.
    Everybody who knows what to do with this data will look for it under the term "bidi class", and it isn't really something anyone can just pick up and use without prior knowledge of what it means. If you do wish to learn more about how to use it, that is also the best term to search for, since the official Unicode property name is "bidi_class".

  • I think Joining_Type should be added as well. It's useful for the same kinds of things.

  • On the implementation side, one of the main ways to optimise Unicode algorithms is to reduce the number of table lookups. It would be nice if the standard library's lookups for Bidi-related properties came from a single mergeable lookup call - so that if I check for a scalar's bidiClass and joiningType, it could potentially be merged in to a single lookup.