# Cannot use \`nil\` or \`.none\` with Optional\<some T\>

**URL:** <https://forums.swift.org/t/cannot-use-nil-or-none-with-optional-some-t/70098>\
**Category:** Using Swift\
**Tags:** optional, opaque-types\
**Created:** [February 16, 2024, 11:09pm UTC](https://forums.swift.org/t/cannot-use-nil-or-none-with-optional-some-t/70098 "2024-02-16T23:09:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![wadetregaskis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wadetregaskis/32/8494_2.png) [@wadetregaskis](https://forums.swift.org/u/wadetregaskis)\
**Post date:** [February 16, 2024, 11:09pm UTC](https://forums.swift.org/t/cannot-use-nil-or-none-with-optional-some-t/70098/1 "2024-02-16T23:09:09Z")

</div>

```swift
struct Wot: View {
    let model: String?

    var body: Optional<some View> {
        guard let model else {
            return nil
        }

        return Text(model)
    }
}

```

> ❌ Underlying type for opaque result type 'Optional' could not be inferred from return expression

Using `.none` instead of `nil` simply changes it to:

> ❌ Type of expression is ambiguous without a type annotation

The code as written seems like it should work, in principle. While I understand _literally_ what the compiler errors say, I don't understand why it thinks it needs me to spell the type information out for it. It's clear what the opaque return type is from the non-nil return.

Is this something that may be fixed in future?

Is there a workaround, in the interim?

[Assume that it's impossible to specify the opaque type even if I wanted to, because in the _real_ world cases where I'm hitting this, that type isn't available to me as it's pre-opaqued by SwiftUI view modifiers, or is a horrendously complicated type that's practically impossible to deduce¹.]

* * *

¹ Tangentially, I _so_ very much wish Swift had a `?` (or whatever) type placeholder like some languages, whereby the compiler / IDE just figures it out for me and amends the code.

---

<div class="post-metadata">

**Author:** ![dmt](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dmt/32/30830_2.png) [@dmt](https://forums.swift.org/u/dmt)\
**Post date:** [February 16, 2024, 11:26pm UTC](https://forums.swift.org/t/cannot-use-nil-or-none-with-optional-some-t/70098/2 "2024-02-16T23:26:34Z")

</div>

> [@wadetregaskis](#):
>
> Is there a workaround, in the interim?

I guess `View` in your example is `SwiftUI.View`? It has `associatedtype Body` which you can use to refer to `body`'s return type.

```swift
struct Wot: View {
  let model: String?

  var body: Optional<some View> {
    guard let model else {
      return Body.none
    }

    return Text(model)
  }
}

```

---

<div class="post-metadata">

**Author:** ![wadetregaskis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wadetregaskis/32/8494_2.png) [@wadetregaskis](https://forums.swift.org/u/wadetregaskis)\
**Post date:** [February 16, 2024, 11:40pm UTC](https://forums.swift.org/t/cannot-use-nil-or-none-with-optional-some-t/70098/3 "2024-02-16T23:40:48Z")

</div>

Oh wow, that's so much easier than I feared. Thanks!

(I still think this extra ceremony shouldn't be necessary, but `Body.none` instead of `nil` isn't too big a deal)

---

<div class="post-metadata">

**Author:** ![smkuehnhold](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/smkuehnhold/32/20361_2.png) [@smkuehnhold](https://forums.swift.org/u/smkuehnhold)\
**Post date:** [February 16, 2024, 11:45pm UTC](https://forums.swift.org/t/cannot-use-nil-or-none-with-optional-some-t/70098/4 "2024-02-16T23:45:19Z")

</div>

Now that begs the question why

```swift
struct Wot: View {
  let model: String?

  var body: Optional<some View> {
    guard let model else {
      return Body.none
    }

    return Text(model)
  }
}

```

can't be replaced with

```swift
struct Wot: View {
  let model: String?

  // error: 'some' types are only permitted in properties, subscripts, and functions
  typealias Body = (some View)?

  var body: Body {
    guard let model else {
      return Body.none
    }

    return Text(model)
  }
}

```

because TIL opaque types can be used to satisfy associatedtype requirements, but only implicitly?

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [February 16, 2024, 11:56pm UTC](https://forums.swift.org/t/cannot-use-nil-or-none-with-optional-some-t/70098/5 "2024-02-16T23:56:05Z")

</div>

Opaque types are not proper types in-and-of themselves. They must, at the point of declaration, have some underlying type that the compiler is able to determine. When detached from a declaration which can provide that type, as in `typealias Body = (some View)?`, the compiler (today) has no way to determine what the underlying type should be. (We don't attempt to 'link' the opaque type to `body` across the typealias.)

---

<div class="post-metadata">

**Author:** ![thai-d-v](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/thai-d-v/32/34054_2.png) [@thai-d-v](https://forums.swift.org/u/thai-d-v)\
**Post date:** [February 17, 2024, 5:55am UTC](https://forums.swift.org/t/cannot-use-nil-or-none-with-optional-some-t/70098/6 "2024-02-17T05:55:12Z")

</div>

> [@wadetregaskis](#):
>
> `Optional<some View>`

What is the purpose of this type? Can you share its `View` conformance code?

---

<div class="post-metadata">

**Author:** ![wadetregaskis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wadetregaskis/32/8494_2.png) [@wadetregaskis](https://forums.swift.org/u/wadetregaskis)\
**Post date:** [February 17, 2024, 6:01am UTC](https://forums.swift.org/t/cannot-use-nil-or-none-with-optional-some-t/70098/7 "2024-02-17T06:01:19Z")

</div>

What do you mean?

---

<div class="post-metadata">

**Author:** ![thai-d-v](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/thai-d-v/32/34054_2.png) [@thai-d-v](https://forums.swift.org/u/thai-d-v)\
**Post date:** [February 17, 2024, 6:33am UTC](https://forums.swift.org/t/cannot-use-nil-or-none-with-optional-some-t/70098/8 "2024-02-17T06:33:21Z")

</div>

Sorry, my bad, I didn't know that `Optional` already conforms to `SwiftUI.View`, that's why I was asking you to share the protocol conformance code. 😭

But I am still curious about the purpose of `Optional<some View>`.

What is the difference between `Optional<some View>` and `_ConditionalContent<Text, EmptyView>` when I can define `Wot` in 2 different ways like these:

```swift
struct Wot: View {
  let model: String?

  var body: Optional<some SwiftUI.View> { // Optional<Text>
    guard let model else {
      return Body.none
    }

    return Text(model)
  }
}

struct Wot: View {
  let model: String?

  var body: some View { // _ConditionalContent<Text, EmptyView>
    if let model { Text(model) } else { EmptyView() }
  }
}

```

---

<div class="post-metadata">

**Author:** ![wadetregaskis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wadetregaskis/32/8494_2.png) [@wadetregaskis](https://forums.swift.org/u/wadetregaskis)\
**Post date:** [February 17, 2024, 4:13pm UTC](https://forums.swift.org/t/cannot-use-nil-or-none-with-optional-some-t/70098/9 "2024-02-17T16:13:21Z")

</div>

I wrote a little about that as part of [Hiding SwiftUI views](https://wadetregaskis.com/hiding-swiftui-views/). Result builders have a lot of limitations (and some annoyances), so it's often nice to disable them. That you can do this with SwiftUI is often overlooked (I myself didn't consciously realise I was in fact doing this until just recently!).
