# Declarative String Processing Overview

**URL:** <https://forums.swift.org/t/declarative-string-processing-overview/52459>\
**Category:** Pitches\
**Created:** [September 29, 2021, 11:13pm UTC](https://forums.swift.org/t/declarative-string-processing-overview/52459 "2021-09-29T23:13:12Z")\
**Posts on this page:** 1\
**Showing post:** 40

<div class="post-metadata">

**Author:** ![1-877-547-7272](https://avatars.discourse-cdn.com/v4/letter/1/278dde/32.png) [@1-877-547-7272](https://forums.swift.org/u/1-877-547-7272)\
**Post date:** [October 1, 2021, 8:09am UTC](https://forums.swift.org/t/declarative-string-processing-overview/52459/40 "2021-10-01T08:09:31Z")

</div>

> [@typesanitizer](#):
>
> Could you share what the types of the core operations (alternation `r1|r2` , sequencing `r1r2` and repetition `r*` ) will be? I see two main directions here:
> 
> 1. Build on top of type sequences.
> 2. Use lots of overloads (SwiftUI style).

I think this is a good time to consider adding union types to the standard library. We could make them using enumerations:

```swift
enum UnionOf2<First, Second> {
    case first(First)
    case second(Second)
}
// as well as UnionOf3, UnionOf4, etc.

```

or we could add union types to the language itself, similarly to tuples:

```swift
let x = (String | Int).0("example")
switch x {
case .0(let string):
    print(0, string)
case .1(let number):
    print(1, number)
}
let namedExample = (string: String | number: Int).string("example")

```

While the second one would be a more complex change, it would allow for infinitely many types within the union.

Union types would provide a good way to express regular expressions like this:

```swift
let r = /(\w+)|(\s+)/
print(type(of: r))
// prints RegEx<UnionOf2<Substring, Substring>>
// or prints RegEx<(Substring | Substring)>

```

Note that these union types aren’t the same as the union types mentioned in the commonly-rejected proposals list — they are fully compatible with Swift’s type system.

Union types have also been recently discussed in the [precise error typing thread](https://forums.swift.org/t/throws-and-throws/4872/41).

* * *

> [@dhoepfl](#):
>
> Will it be possible to change the `/` delimiter?  
> It is annoying if you have to escape each occurance of a path separator.  
> Maybe similar to Strings? [1-877-547-7272’s proposal](https://forums.swift.org/t/declarative-string-processing-overview/52459/17) would work (I’d use `#/.../#` though).

Yeah, after further consideration, I don’t think it’s a good idea to reuse `"` — regexes are different enough from strings that we’d want different syntax highlighting, and we wouldn’t be able to highlight regexes differently than strings if we reused string literal syntax (unless we added type inference to the syntax highlighting algorithm, which would create its own problems). I think `#/.../#` is an acceptable syntax, though we should still try to think of alternatives. Ideally the syntax we end up using would support a raw string–like syntax (i.e. `#####/file/path/#####`). I don’t think it’s as important to have multi-line syntax or inline comments — `Pattern`s can be used to achieve a similar effect and inline comments aren’t allowed within multi-line strings anyway.

```swift
line.match {
    /([0-9A-F]+)/ // Hex scalar
    /(?:\.\.([0-9A-F]+))?/ // Optional scalar range
    /\s*;\s/ // Separator
    /(\w+)/ // Property
    /.*/ // Ignore rest
}

```

---

_[View the full topic](https://forums.swift.org/t/declarative-string-processing-overview/52459)._
