# Is it possible to use protocols with associated types as opaque return types?

**URL:** <https://forums.swift.org/t/is-it-possible-to-use-protocols-with-associated-types-as-opaque-return-types/36605>\
**Category:** Using Swift\
**Created:** [May 18, 2020, 1:55am UTC](https://forums.swift.org/t/is-it-possible-to-use-protocols-with-associated-types-as-opaque-return-types/36605 "2020-05-18T01:55:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iansampson](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/iansampson/32/11737_2.png) [@iansampson](https://forums.swift.org/u/iansampson)\
**Post date:** [May 18, 2020, 1:55am UTC](https://forums.swift.org/t/is-it-possible-to-use-protocols-with-associated-types-as-opaque-return-types/36605/1 "2020-05-18T01:55:12Z")

</div>

I.e. is it possible to use a return type such as `some A where A.B == C`?

My hunch is no, but I’d hoped it might be (or could be in the future), given that the [original pitch](https://forums.swift.org/t/opaque-result-types/15645) cites LazyMapCollection (with associated type Element) as a compelling example of why opaque types are needed.

Context: I’m building a parsing library that uses function builders to provide a clean syntax for combining parsers, which all conform to a Parser protocol. The simplified code snippet below illustrates the problem:

```swift
// ERROR: 'Parsable' requires the types 'MyParsable.S' (aka 'Substring')
// and '(some Parser).S' be equivalent
struct MyParsable: Parsable {
    typealias S = Substring
    var body: some Parser {
        MyParser()
    }
}

struct MyParser: Parser {
    func parse(_ input: Substring) -> (Substring, Character)? {
        var remainingInput = input
        return remainingInput.popFirst().map {
            (remainingInput, $0)
        }
    }
}

protocol Parser {
    associatedtype S : Collection where S.SubSequence == S
    associatedtype R
    func parse(_ input: S) -> (S.SubSequence, R)?
}

protocol Parsable: Parser where P.S == S {
    associatedtype P : Parser
    var body: P { get }
}

extension Parsable {
    func parse(_ input: S) -> (S, P.R)? {
        body.parse(input)
    }
}

```

I’m trying to find a way to constrain the `some Parser` return type in `MyParsable` to make `(some Parser).S` and `MyParsable.S` equivalent. Any help will be much appreciated :).

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [May 18, 2020, 3:18am UTC](https://forums.swift.org/t/is-it-possible-to-use-protocols-with-associated-types-as-opaque-return-types/36605/2 "2020-05-18T03:18:10Z")

</div>

> [@iansampson](#):
>
> I’m trying to find a way to constrain the `some Parser` return type in `MyParsable` to make `(some Parser).S` and `MyParsable.S` equivalent. Any help will be much appreciated :).

Constraining `some Parser` is a feature not implemented in the current version of Swift.

---

<div class="post-metadata">

**Author:** ![iansampson](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/iansampson/32/11737_2.png) [@iansampson](https://forums.swift.org/u/iansampson)\
**Post date:** [May 18, 2020, 3:35pm UTC](https://forums.swift.org/t/is-it-possible-to-use-protocols-with-associated-types-as-opaque-return-types/36605/3 "2020-05-18T15:35:12Z")

</div>

Good to know, thanks for the response. I wonder if there are any plans to implement this in the future?

For now I’ll probably just use `let parser = ...` (with type inference, so users of the library don’t have to type out the incredibly complex types of nested parsers).
