# Opaque result type question

**URL:** <https://forums.swift.org/t/opaque-result-type-question/33425>\
**Category:** Using Swift\
**Tags:** swiftui, opaque-types\
**Created:** [February 4, 2020, 3:43pm UTC](https://forums.swift.org/t/opaque-result-type-question/33425 "2020-02-04T15:43:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mackoj](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mackoj/32/42395_2.png) [@mackoj](https://forums.swift.org/u/mackoj)\
**Post date:** [February 4, 2020, 3:43pm UTC](https://forums.swift.org/t/opaque-result-type-question/33425/1 "2020-02-04T15:43:45Z")

</div>

Hi,

Why the first part work and not the second ?

```
import SwiftUI

// Why does this works

enum Blueprint {
  case boeing747
  case concorde
}

protocol Plane { }
struct Boeing747: Plane { }
struct Concorde: Plane { }

func factory(_ blueprint : Blueprint) -> some Plane {
  return Boeing747()
}

// And this does not work ?

enum Screen {
  case homepage
  case setting
  case loggin
}

struct HomePage: View { var body: some View { Text("Homepage") } }
struct Setting: View { var body: some View { Text("Setting") } }
struct Loggin: View { var body: some View { Text("Loggin") } }

func screenBuilder(_ screen : Screen) -> some View {
  switch screen {
  case .homepage:
    return HomePage()
  default: break
  }
  return Text("Not Implemented")
}

```

I don't understand why I have this error.

```
error: SwiftUIRouter.playground:37:6: error: function declares an opaque return type, but the return statements in its body do not have matching underlying types
func screenBuilder(_ screen : Screen) -> some View {
     ^

SwiftUIRouter.playground:40:12: note: return statement has underlying type 'HomePage'
    return HomePage()
           ^

SwiftUIRouter.playground:43:10: note: return statement has underlying type 'Text'
  return Text("Not Implemented")

```

---

<div class="post-metadata">

**Author:** ![Alejandro](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alejandro/32/46197_2.png) [@Alejandro](https://forums.swift.org/u/Alejandro)\
**Post date:** [February 4, 2020, 3:47pm UTC](https://forums.swift.org/t/opaque-result-type-question/33425/2 "2020-02-04T15:47:47Z")

</div>

You’re returning two different types of Views, only 1 type of View can be returned.

---

<div class="post-metadata">

**Author:** ![mackoj](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mackoj/32/42395_2.png) [@mackoj](https://forums.swift.org/u/mackoj)\
**Post date:** [February 4, 2020, 3:51pm UTC](https://forums.swift.org/t/opaque-result-type-question/33425/3 "2020-02-04T15:51:26Z")

</div>

Ok but opaque result type should allow me to return objects of different type that conform to the same protocol ? No ?

Because this won't work either.

```swift
func screenBuilder<T : View>(_ screen : Screen) -> T {
  switch screen {
  case .homepage:
    return HomePage()
  default: break
  }
  return Text("Not Implemented")
}

```

---

<div class="post-metadata">

**Author:** ![Avi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/avi/32/2962_2.png) [@Avi](https://forums.swift.org/u/Avi)\
**Post date:** [February 4, 2020, 3:56pm UTC](https://forums.swift.org/t/opaque-result-type-question/33425/4 "2020-02-04T15:56:15Z")

</div>

> [@mackoj](#):
>
> No ?

No.

Opaque result types only hide the type from the caller. The callee still has to honor the contract, which is that a function returning an opaque type always returns the same concrete type.

---

<div class="post-metadata">

**Author:** ![mackoj](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mackoj/32/42395_2.png) [@mackoj](https://forums.swift.org/u/mackoj)\
**Post date:** [February 4, 2020, 3:56pm UTC](https://forums.swift.org/t/opaque-result-type-question/33425/5 "2020-02-04T15:56:48Z")

</div>

Okay I will find another way thanks

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [February 4, 2020, 4:32pm UTC](https://forums.swift.org/t/opaque-result-type-question/33425/6 "2020-02-04T16:32:05Z")

</div>

Use the @ViewBuilder If/Else:

```swift
@ViewBuilder
func screenBuilder(_ screen : Screen) -> some View {
    if screen == .homepage: {
        HomePage()
    } else {
        Text("Not Implemented")
    }
}

```

You can keep adding `else if` to handle any number of "cases".

Unfortunately the Swift compiler in Xcode 11.3.1 is a little buggy now and the above code may not conpile. See [I'm stumped with this @ViewBuilder on var body: some View compile error: Cannot convert return expression of type '\_ConditionalContent\<Text, Text\>' to return type 'some View'](https://forums.swift.org/t/im-stumped-with-this-viewbuilder-on-var-body-some-view-compile-error-cannot-convert-return-expression-of-type-conditionalcontent-text-text-to-return-type-some-view/33258)

You can get around the bug by first create an variable and use that in the If/Else:

```swift
let homePage = HomePage() // <== work around the bug
let text = Text("Not Implemented")
@ViewBuilder
func screenBuilder(_ screen : Screen) -> some View {
    if screen == .homepage: {
        homePage // <== use this instead
    } else {
        text
    }
}

```

---

<div class="post-metadata">

**Author:** ![mackoj](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mackoj/32/42395_2.png) [@mackoj](https://forums.swift.org/u/mackoj)\
**Post date:** [February 4, 2020, 5:49pm UTC](https://forums.swift.org/t/opaque-result-type-question/33425/7 "2020-02-04T17:49:44Z")

</div>

I have tried what you proposed before it compiled but don't work properly.

It was like the return did not work properly.

I will try it again, thanks.
