# Advice Needed for Making Use of Generics/Opaques/Existentials

**URL:** <https://forums.swift.org/t/advice-needed-for-making-use-of-generics-opaques-existentials/69683>\
**Category:** Using Swift\
**Tags:** generics, opaque-types, existential, protocol-inheritance\
**Created:** [January 28, 2024, 10:36pm UTC](https://forums.swift.org/t/advice-needed-for-making-use-of-generics-opaques-existentials/69683 "2024-01-28T22:36:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![oguzhanvarsak](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/oguzhanvarsak/32/33843_2.png) [@oguzhanvarsak](https://forums.swift.org/u/oguzhanvarsak)\
**Post date:** [January 28, 2024, 10:36pm UTC](https://forums.swift.org/t/advice-needed-for-making-use-of-generics-opaques-existentials/69683/1 "2024-01-28T22:36:54Z")

</div>

Hello Swift community.

I am trying to make use of generics/opaque/existential in an architecture built using protocols, but getting conformance errors here and there.

I can't figure out what is the core issue here, and can't find other examples to fix.

Let me try to explain what is going on:

I have a base class that contains some basic functionality all new instances can make use.

```swift
class FooUnit: FooContainable {
  let a: A

  init(a: A) {
    self.a = a
  }

  func doThisCommonThing() {
  // Stuff
  }
}

```

And it conforms to this protocol:

```swift
protocol FooContainable: AnyObject {
  var y: XCUIApplication { get }
 
  func createFoo<T: Foo>() -> T
}

extension FooContainable {
  func createFoo<T: Foo>() -> {
    let z = T(a: A) // Initiates `TFoo`

    return z
  }
}

```

The `Foo` here is just a type alias combining 2 protocols, which is all the new classes inherit:

```swift
typealias Foo = FooUnit & FooTestable

```

```swift
protocol FooTestable: SomeUnrelatedProtocol {
  var x: Element { get }
}

```

An example from of how these protocols used in codebase:

```swift
final class FirstFoo: Foo {
    var x: Element {
        application.otherElements["Q_View"]
    }

    func doThis() -> FirstFoo {
       // Stuff
      return self
    }

    func returnSecondFoo() -> SecondFoo {
        return createFoo() // Initiates and Returns `SecondFoo`
    }
}

```

And this `Foo` classes used like this:

```swift
final class SomeClass {
    func doThat() {
        FirstFoo(a: A)
            .doThis()
            .returnSecondFoo()
            .doSomethingFromSecondFoo()
    }
}

```

Now coming to my issue.

I want to create a function with generic/opaque/existential return type:

```swift
final class ThirdFoo: Foo {
    var x: Element {
        application.otherElements["E_View"]
    }

    func doElse<T: Flow>() -> T {
       // Stuff
      return createFoo() // Initiate and Return Caller's `Foo`
    }
}

```

But unfortunately I can't achieve this and get this complaint:

```swift
final class SomeClass {
    func doThat() {
        FirstFoo(a: A)
            .returnThirdFoo()
            .doSomethingFromThirdFooThatReturnsT() // Instance method 'doElse()' requires that 'FooUnit' conform to 'FooTestable'
    }
}

```

I am a bit lost and seek advice where am I doing wrong.

Thanks for any suggestions!

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [January 29, 2024, 1:07am UTC](https://forums.swift.org/t/advice-needed-for-making-use-of-generics-opaques-existentials/69683/2 "2024-01-29T01:07:50Z")

</div>

It's hard for us to see because the code is neither compileable nor self-contained (e.g. the XCUIApplication dependency). I'd recommend make it much simpler and compilable up to the point that shows the single compilation error you are talking about.

---

<div class="post-metadata">

**Author:** ![oguzhanvarsak](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/oguzhanvarsak/32/33843_2.png) [@oguzhanvarsak](https://forums.swift.org/u/oguzhanvarsak)\
**Post date:** [January 29, 2024, 7:15pm UTC](https://forums.swift.org/t/advice-needed-for-making-use-of-generics-opaques-existentials/69683/3 "2024-01-29T19:15:08Z")

</div>

Here is a snippet you can put into Playground and get the same error message:

```swift
class FooUnit: FooContainable {
    var application: Int
    
    init(application: Int) {
        self.application = application
    }
    
    func doThisCommonThing() {
        // Stuff
    }
}

protocol FooTestable: BooTestable {
    var fooEntryPoint: Double { get }
    
    func doFooStuff()
}

extension FooTestable {
    func doFooStuff() { }
}

protocol BooTestable: AnyObject {
    var application: Int { get }
    
    init(application: Int)
}

protocol FooContainable: AnyObject {
    var application: Int { get }
    
    func createFoo<T: Foo>() -> T
}

extension FooContainable {
    func createFoo<T: Foo>() -> T {
        let z = T(application: application) // Initiates `TFoo`
        
        return z
    }
}

typealias Foo = FooUnit & FooTestable

final class FirstFoo: Foo {
    var fooEntryPoint: Double {
        13
    }
    
    func doThis() -> FirstFoo {
        // Stuff
        return self
    }
    
    func returnSecondFoo() -> SecondFoo {
        return createFoo() // Initiates and Returns `SecondFoo`
    }
}

final class SecondFoo: Foo {
    var fooEntryPoint: Double {
        14
    }
    
    func returnFirstFoo() -> FirstFoo {
        return createFoo()
    }
    
    func returnThirdFoo() -> ThirdFoo {
        return createFoo()
    }
}

final class ThirdFoo: Foo {
    var fooEntryPoint: Double {
        15
    }
    
    func doElse<T: Foo>() -> T {
        return createFoo() // Initiate and Return Caller's `Foo`
    }
}

final class SomeClass {
    func doThat() {
        FirstFoo(application: 13)
            .doThis()
            .returnSecondFoo()
            .returnThirdFoo()
            .doElse() // Instance method 'doElse()' requires that 'FooUnit' conform to 'FooTestable'
    }
}

```
