# \#optional

**URL:** https://forums.swift.org/tag/optional/64.md

[Latest](https://forums.swift.org/latest.md) · [Categories](https://forums.swift.org/categories.md) · [Tags](https://forums.swift.org/tags.md)

---

## [Meaning of Optional \<Optional \<Optional \<T\>\>\>](https://forums.swift.org/t/meaning-of-optional-optional-optional-t/87122)

<div class="topic-metadata">

**Author:** [@ibex](https://forums.swift.org/u/ibex)\
**Replies:** 9\
**Last updated:** [June 5, 2026, 7:34pm UTC](https://forums.swift.org/t/meaning-of-optional-optional-optional-t/87122 "2026-06-05T19:34:26Z")

</div>

Given: Optional \<T\> // nil or T Optional \<Optional \<T\>\> Optional \<Optional \<Optional \<T\>\>\> Optional \<Optional \<Optional \<Optional \<T\>\>\>\> ... Would it be incorrect to say "They are, effectively, all Optional \<T\>" ? A…

---

## [\[Pitch\] Optional noncopyable improvements and generalizations](https://forums.swift.org/t/pitch-optional-noncopyable-improvements-and-generalizations/86656)

<div class="topic-metadata">

**Author:** [@Alejandro](https://forums.swift.org/u/Alejandro)\
**Replies:** 13\
**Last updated:** [May 26, 2026, 11:16pm UTC](https://forums.swift.org/t/pitch-optional-noncopyable-improvements-and-generalizations/86656 "2026-05-26T23:16:42Z")

</div>

Hi Evolution, I have a short and sweet Optional proposal that adds some new methods when working with noncopyable wrapped types as well as some generalizations to existing API to work with noncopyables. Please let me kn…

---

## [No way to create a non-nil std.optional in Swift](https://forums.swift.org/t/no-way-to-create-a-non-nil-std-optional-in-swift/72101)

<div class="topic-metadata">

**Author:** [@Drejzer](https://forums.swift.org/u/Drejzer)\
**Replies:** 6\
**Last updated:** [March 18, 2026, 12:51pm UTC](https://forums.swift.org/t/no-way-to-create-a-non-nil-std-optional-in-swift/72101 "2026-03-18T12:51:00Z")

</div>

I'm working with a C++ library that has methods taking std::optionals (sometimes multiple std::optional parameters in a row). I noticed that there isn't really any built-in way to build a c++ optional in swift. Since th…

---

## [Operator to avoid "Left side of nil coalescing operator '?' has non-optional type"](https://forums.swift.org/t/operator-to-avoid-left-side-of-nil-coalescing-operator-has-non-optional-type/85067)

<div class="topic-metadata">

**Author:** [@sspringer](https://forums.swift.org/u/sspringer)\
**Replies:** 2\
**Last updated:** [March 1, 2026, 6:38pm UTC](https://forums.swift.org/t/operator-to-avoid-left-side-of-nil-coalescing-operator-has-non-optional-type/85067 "2026-03-01T18:38:44Z")

</div>

(Note that the title should actually be: Operator to avoid "Left side of nil coalescing operator '??' has non-optional type" but one ? disappears when saving the topic.) To avoid the warning "Left side of nil coalescing…

---

## [An optional initializer was treated as definitive?!](https://forums.swift.org/t/an-optional-initializer-was-treated-as-definitive/85058)

<div class="topic-metadata">

**Author:** [@CTMacUser](https://forums.swift.org/u/CTMacUser)\
**Replies:** 0\
**Last updated:** [March 1, 2026, 12:17am UTC](https://forums.swift.org/t/an-optional-initializer-was-treated-as-definitive/85058 "2026-03-01T00:17:01Z")

</div>

May be related to this old post. This is from my testing code: // Invalid should fail // // Replacing the second "initializer" with "NameToken" will make // "digitStarter" to be \`NameToken\` \_instead\_ of \`NameToken?\`, //…

---

## [Swift-frontend crash switching over (any Error)?](https://forums.swift.org/t/swift-frontend-crash-switching-over-any-error/84670)

<div class="topic-metadata">

**Author:** [@bbrk24](https://forums.swift.org/u/bbrk24)\
**Replies:** 4\
**Last updated:** [February 11, 2026, 2:38am UTC](https://forums.swift.org/t/swift-frontend-crash-switching-over-any-error/84670 "2026-02-11T02:38:53Z")

</div>

Here's the code: var error: Error? // ... something which may or may not set error ... switch error { case AFError.explicitlyCancelled: // Task was cancelled, don't continue return case let e?: debugPrint(e…

---

## [Mutating an Optional-wrapped value type](https://forums.swift.org/t/mutating-an-optional-wrapped-value-type/84582)

<div class="topic-metadata">

**Author:** [@CTMacUser](https://forums.swift.org/u/CTMacUser)\
**Replies:** 3\
**Last updated:** [February 10, 2026, 3:12pm UTC](https://forums.swift.org/t/mutating-an-optional-wrapped-value-type/84582 "2026-02-10T15:12:49Z")

</div>

If I want to mutate a value that’s within an Optional, I’d have to use an if-var or similar, mutate that copy, then copy it back? I did find out that mutating instance methods can work through a myVariable?.selfMutatingM…

---

## [Limitations of \`borrowing\` / why not first-class borrowed references?](https://forums.swift.org/t/limitations-of-borrowing-why-not-first-class-borrowed-references/80025)

<div class="topic-metadata">

**Author:** [@KeithBauerANZ](https://forums.swift.org/u/KeithBauerANZ)\
**Replies:** 4\
**Last updated:** [February 4, 2026, 9:19pm UTC](https://forums.swift.org/t/limitations-of-borrowing-why-not-first-class-borrowed-references/80025 "2026-02-04T21:19:10Z")

</div>

struct Noncopyable: ~Copyable {} func doSomething(\_ n: borrowing Noncopyable? = nil) {} let n = Noncopyable() doSomething(n) // error: implicit conversion to 'Noncopyable?' is consuming Yeah, makes sense. I understand…

---

## [Magic of hiding the type](https://forums.swift.org/t/magic-of-hiding-the-type/83896)

<div class="topic-metadata">

**Author:** [@nikolaknez](https://forums.swift.org/u/nikolaknez)\
**Replies:** 3\
**Last updated:** [January 2, 2026, 8:38pm UTC](https://forums.swift.org/t/magic-of-hiding-the-type/83896 "2026-01-02T20:38:45Z")

</div>

Is my understanding correct that the “magic” of hiding the type happens in init ( we only disclose it on init time) here : extension AnySequence: Sendable {} extension AnySequence: Sequence { public typealias Iterator…

---

## [On chains, nils, and quiet failures](https://forums.swift.org/t/on-chains-nils-and-quiet-failures/83799)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 4\
**Last updated:** [December 23, 2025, 2:00am UTC](https://forums.swift.org/t/on-chains-nils-and-quiet-failures/83799 "2025-12-23T02:00:35Z")

</div>

There's a ton of optional chains in my app (foo?.bar?.baz?.qux()) and they are a great tool to keep the source concise, however some of those ? might fail silently and the failures might go completely unnoticed. In addit…

---

## [Mysterious Default Value for un-assigned variable](https://forums.swift.org/t/mysterious-default-value-for-un-assigned-variable/82670)

<div class="topic-metadata">

**Author:** [@MojtabaHs](https://forums.swift.org/u/MojtabaHs)\
**Replies:** 22\
**Last updated:** [October 16, 2025, 11:23am UTC](https://forums.swift.org/t/mysterious-default-value-for-un-assigned-variable/82670 "2025-10-16T11:23:26Z")

</div>

Hello everyone :waving\_hand: I came across a real mind-twist in Swift, and I’d love to hear your thoughts. Take a look at this code (no escaping, no concurrency involved): func foo(date: Date, action: () -\> Void) -\> D…

---

## [\[Pitch\] JSONDecoder.nullDecodingStrategy](https://forums.swift.org/t/pitch-jsondecoder-nulldecodingstrategy/13980)

<div class="topic-metadata">

**Author:** [@Dan\_Stenmark](https://forums.swift.org/u/Dan_Stenmark)\
**Replies:** 19\
**Last updated:** [August 16, 2025, 7:58pm UTC](https://forums.swift.org/t/pitch-jsondecoder-nulldecodingstrategy/13980 "2025-08-16T19:58:38Z")

</div>

JSONDecoder currently does not differentiate between a payload containing an explicit null or the field being omitted. I would like to propose an additive change to include a decoding strategy for these situations: cla…

---

## [\[Pre-Pitch\] Disconnected Properties and Variables, and Opaque enums](https://forums.swift.org/t/pre-pitch-disconnected-properties-and-variables-and-opaque-enums/81355)

<div class="topic-metadata">

**Author:** [@metaphorce42](https://forums.swift.org/u/metaphorce42)\
**Replies:** 12\
**Last updated:** [July 31, 2025, 9:25pm UTC](https://forums.swift.org/t/pre-pitch-disconnected-properties-and-variables-and-opaque-enums/81355 "2025-07-31T21:25:00Z")

</div>

Currently in Swift 6.2, you cannot send an object out of an actor or Task if it has been bound to a property or captured. Also you cannot send a non-Sendable object from a synchronous context into an asynchronous context…

---

## ["Removing implicit \`nil\` initial value for Optional-typed variables"](https://forums.swift.org/t/removing-implicit-nil-initial-value-for-optional-typed-variables/64413)

<div class="topic-metadata">

**Author:** [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Replies:** 28\
**Last updated:** [June 12, 2025, 5:17pm UTC](https://forums.swift.org/t/removing-implicit-nil-initial-value-for-optional-typed-variables/64413 "2025-06-12T17:17:39Z")

</div>

A while ago I read this line of this post by Slava Pestov and it stuck with me. Today I felt motivated to follow-up on this idea, and I found this thread started by Jordan Rose: which I see ended with @tera's unansw…

---

## [Opposite of Optional\<T\>.take() to set to .some value iff nil](https://forums.swift.org/t/opposite-of-optional-t-take-to-set-to-some-value-iff-nil/77446)

<div class="topic-metadata">

**Author:** [@ctietze](https://forums.swift.org/u/ctietze)\
**Replies:** 15\
**Last updated:** [January 31, 2025, 2:58am UTC](https://forums.swift.org/t/opposite-of-optional-t-take-to-set-to-some-value-iff-nil/77446 "2025-01-31T02:58:45Z")

</div>

mutating func take() -\> Wrapped? is neat. I wonder what's a good name for an opposite operation, one that sets it to .some value iff it's .none? The desired effect would result in transforming this: self.myOptionalValu…

---

## [Apparent compiler bug relating to Optional witnessing failable initializer requirement](https://forums.swift.org/t/apparent-compiler-bug-relating-to-optional-witnessing-failable-initializer-requirement/76759)

<div class="topic-metadata">

**Author:** [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Replies:** 1\
**Last updated:** [January 2, 2025, 7:44pm UTC](https://forums.swift.org/t/apparent-compiler-bug-relating-to-optional-witnessing-failable-initializer-requirement/76759 "2025-01-02T19:44:48Z")

</div>

I think this must be a bug. The following test passes if the Optional initializer is declared as a failable initializer, but fails if the ? is removed from the Optional initializer. The Test @Test func foo() { #expe…

---

## [Possible bug in Swift?](https://forums.swift.org/t/possible-bug-in-swift/76293)

<div class="topic-metadata">

**Author:** [@F1248](https://forums.swift.org/u/F1248)\
**Replies:** 2\
**Last updated:** [November 29, 2024, 10:02pm UTC](https://forums.swift.org/t/possible-bug-in-swift/76293 "2024-11-29T22:02:53Z")

</div>

The following code prints nil although I would expect it to print 0. I know it could be simplified to work around the bug, but this is already the simplified possible version of a complex code that requires this structu…

---

## [A More Swifty Way to Check an Optional for a Nil Value](https://forums.swift.org/t/a-more-swifty-way-to-check-an-optional-for-a-nil-value/19221)

<div class="topic-metadata">

**Author:** [@rlziii](https://forums.swift.org/u/rlziii)\
**Replies:** 61\
**Last updated:** [October 9, 2024, 4:17pm UTC](https://forums.swift.org/t/a-more-swifty-way-to-check-an-optional-for-a-nil-value/19221 "2024-10-09T16:17:20Z")

</div>

Swift does a great job handling optional values. Initially a learning curve for new developers, the power and value of using optionals quickly becomes apparent when writing safe, thoughtful code. Swift offers up many w…

---

## [How to unwrap an optional option (ie 'var window?')](https://forums.swift.org/t/how-to-unwrap-an-optional-option-ie-var-window/52636)

<div class="topic-metadata">

**Author:** [@dhoerl](https://forums.swift.org/u/dhoerl)\
**Replies:** 12\
**Last updated:** [September 6, 2024, 6:18am UTC](https://forums.swift.org/t/how-to-unwrap-an-optional-option-ie-var-window/52636 "2024-09-06T06:18:00Z")

</div>

What I want to use: if let window = UIApplication.shared.delegate?.window { but the compiler won't let me, so I found a work-around: if let \_window = UIApplication.shared.delegate?.window, let window = \_window { S…

---

## [\`Cannot find type 'Key' in scope\` when extending optional Dictionaries](https://forums.swift.org/t/cannot-find-type-key-in-scope-when-extending-optional-dictionaries/72706)

<div class="topic-metadata">

**Author:** [@F1248](https://forums.swift.org/u/F1248)\
**Replies:** 10\
**Last updated:** [June 24, 2024, 6:39pm UTC](https://forums.swift.org/t/cannot-find-type-key-in-scope-when-extending-optional-dictionaries/72706 "2024-06-24T18:39:31Z")

</div>

I'm trying to write this simple extension for generic optional Dictionaries with unspecified Keys and Values, both optional. The following works fine for non-optional Dictionaries: extension Dictionary { func conta…

---

## [\`if let x as? T\`](https://forums.swift.org/t/if-let-x-as-t/60922)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 14\
**Last updated:** [April 7, 2024, 2:31pm UTC](https://forums.swift.org/t/if-let-x-as-t/60922 "2024-04-07T14:31:07Z")

</div>

Now that we have if let x shortcut, shouldn't we also have: if let value as? T as a shortcut for if let value = value as? T As a small data point – in one of my big projects of all if-let-foos (hundreds of them) abou…

---

## [Optional\<Error\>'s localizedDescription vs error.debugDescription](https://forums.swift.org/t/optional-error-s-localizeddescription-vs-error-debugdescription/70274)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 1\
**Last updated:** [February 26, 2024, 5:10am UTC](https://forums.swift.org/t/optional-error-s-localizeddescription-vs-error-debugdescription/70274 "2024-02-26T05:10:40Z")

</div>

Quite odd behaviour, isn't it? var error: Error? \_ = error!.localizedDescription // ✅ \_ = error.debugDescription // ✅ \_ = error.localizedDescription // 🛑 Value of optional type '(any Error)?' must be unwrapped t…

---

## [Cannot use \`nil\` or \`.none\` with Optional\<some T\>](https://forums.swift.org/t/cannot-use-nil-or-none-with-optional-some-t/70098)

<div class="topic-metadata">

**Author:** [@wadetregaskis](https://forums.swift.org/u/wadetregaskis)\
**Replies:** 8\
**Last updated:** [February 17, 2024, 4:13pm UTC](https://forums.swift.org/t/cannot-use-nil-or-none-with-optional-some-t/70098 "2024-02-17T16:13:21Z")

</div>

struct Wot: View { let model: String? var body: Optional\<some View\> { guard let model else { return nil } return Text(model) } } :x: Underlying type for opaque result t…

---

## [How does the optional chaining work here](https://forums.swift.org/t/how-does-the-optional-chaining-work-here/69850)

<div class="topic-metadata">

**Author:** [@Alc](https://forums.swift.org/u/Alc)\
**Replies:** 3\
**Last updated:** [February 6, 2024, 7:15am UTC](https://forums.swift.org/t/how-does-the-optional-chaining-work-here/69850 "2024-02-06T07:15:46Z")

</div>

I do not seem to understand how optional chaining works. The first print works as I expect. The second works, but how and why? The third one fails, but I thought I was accessing .count on Optional("") because that is wha…

---

## [Can anyone explain this behavior in Optional Chaining?](https://forums.swift.org/t/can-anyone-explain-this-behavior-in-optional-chaining/69679)

<div class="topic-metadata">

**Author:** [@Alc](https://forums.swift.org/u/Alc)\
**Replies:** 11\
**Last updated:** [January 29, 2024, 11:39pm UTC](https://forums.swift.org/t/can-anyone-explain-this-behavior-in-optional-chaining/69679 "2024-01-29T23:39:33Z")

</div>

Documentation states the following Specifically, the result of an optional chaining call is of the same type as the expected return value, but wrapped in an optional. A property that normally returns an Int will return…

---

## [\`Bool?\` is not optional?](https://forums.swift.org/t/bool-is-not-optional/67289)

<div class="topic-metadata">

**Author:** [@bbrk24](https://forums.swift.org/u/bbrk24)\
**Replies:** 5\
**Last updated:** [September 15, 2023, 7:22pm UTC](https://forums.swift.org/t/bool-is-not-optional/67289 "2023-09-15T19:22:08Z")

</div>

What in the type inference is going on here? (thread.stack is an array) warning: left side of nil coalescing operator '??' has non-optional type 'Bool?', so the right side is never used let goLeft = { thread.sta…

---

## [Comparing result of \`try?\`](https://forums.swift.org/t/comparing-result-of-try/66937)

<div class="topic-metadata">

**Author:** [@bbrk24](https://forums.swift.org/u/bbrk24)\
**Replies:** 3\
**Last updated:** [August 25, 2023, 3:34am UTC](https://forums.swift.org/t/comparing-result-of-try/66937 "2023-08-25T03:34:32Z")

</div>

I want to check whether the result of a throwing function is a specific value, while suppressing errors: enum E { case foo, bar } func baz() throws -\> E { // ... } if try? baz() == .foo { // ... } However, this…

---

## [Optional \`ExpressibleByWrappedValue\` superpowers](https://forums.swift.org/t/optional-expressiblebywrappedvalue-superpowers/66358)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 19\
**Last updated:** [July 28, 2023, 9:47pm UTC](https://forums.swift.org/t/optional-expressiblebywrappedvalue-superpowers/66358 "2023-07-28T21:47:44Z")

</div>

What does the standard library do to make wrapped values compatible with optionals? I want the same for my type: enum MyOptional\<Wrapped\> { case none case some(Wrapped) } struct S {} var x: MyOptional\<S\> = S() …

---

## [Implicitly unwrapped optionals in tuples](https://forums.swift.org/t/implicitly-unwrapped-optionals-in-tuples/66434)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 2\
**Last updated:** [July 28, 2023, 3:05pm UTC](https://forums.swift.org/t/implicitly-unwrapped-optionals-in-tuples/66434 "2023-07-28T15:05:25Z")

</div>

I can use implicitly unwrapped optionals in structs and enums but not tuples: struct A { var x: Int!, y: Float! } // ✅ enum B { case x(Int!), y(Float!) } // ✅ typealias C = (x: Int!, y: Float!) // 🛑 Using '!' is not a…

---

## [\[Pitch\] Extending optional chains to include for loops](https://forums.swift.org/t/pitch-extending-optional-chains-to-include-for-loops/65848)

<div class="topic-metadata">

**Author:** [@sspringer](https://forums.swift.org/u/sspringer)\
**Replies:** 51\
**Last updated:** [July 24, 2023, 3:11pm UTC](https://forums.swift.org/t/pitch-extending-optional-chains-to-include-for-loops/65848 "2023-07-24T15:11:28Z")

</div>

This is about optional chains used as the "sequence expression" in a for-in loop. After some recent discussion in another topic, I tried to start to start a more formal pitch there: Please note: This is just my (hu…

[Next page](https://forums.swift.org/tag/optional/64.md?match_all_tags=true&page=1&tags%5B%5D=optional)
