# \[Pitch\] nonoptional set

**URL:** <https://forums.swift.org/t/pitch-nonoptional-set/68265>\
**Category:** Pitches\
**Created:** [November 5, 2023, 5:54pm UTC](https://forums.swift.org/t/pitch-nonoptional-set/68265 "2023-11-05T17:54:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![davdroman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/davdroman/32/46501_2.png) [@davdroman](https://forums.swift.org/u/davdroman)\
**Post date:** [November 5, 2023, 5:54pm UTC](https://forums.swift.org/t/pitch-nonoptional-set/68265/1 "2023-11-05T17:54:56Z")

</div>

At times it makes sense to have a computed property return optional values whilst only accepting non-optional values. For example, for a `Validation` property wrapper:

```swift
// vastly simplified example
@propertyWrapper
struct Validation {
    private var input: String = ""
    var wrappedValue: String? {
        get {
            if !input.isEmpty {
                return input
            } else {
                return nil
            }
        }
        nonoptional set {
            input = newValue
        }
    }
}

@Validation var name = ""
print(name) // nil, because an empty string is invalid input
name = "David"
print(name) // "David", because it's a valid input
name = nil // 🛑 error: property cannot be explicitly set to nil

```

This would also help with certain things like enum extraction/embedding which follows this same prism optic, although that's been extensively discussed in [a dedicated thread](https://forums.swift.org/t/discussion-inclusion-of-casepaths-into-the-language/53024/9) so I won't give any specific examples here.

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [November 5, 2023, 5:57pm UTC](https://forums.swift.org/t/pitch-nonoptional-set/68265/2 "2023-11-05T17:57:42Z")

</div>

Here's the most recent previous thread on this, though coming at it from the other direction. :-)

> [@\[Pitch\] Properties and subscripts with optional getters](https://forums.swift.org/t/pitch-properties-and-subscripts-with-optional-getters/65536/):
>
> Hey folks, I'm sharing an early draft of a pitch for adding optional getters to Swift. Its use is similar to updating a property through optional chaining syntax. Appreciate the early feedback as this pitch gets refined! Properties and subscripts with optional getters Introduction Computed properties and subscripts in Swift require both the getter and setter to be of the same type. This is intuitive and sufficient for most cases. However, there are times where we might prefer that the getter …

---

<div class="post-metadata">

**Author:** ![davdroman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/davdroman/32/46501_2.png) [@davdroman](https://forums.swift.org/u/davdroman)\
**Post date:** [November 5, 2023, 6:00pm UTC](https://forums.swift.org/t/pitch-nonoptional-set/68265/3 "2023-11-05T18:00:31Z")

</div>

Oh I totally missed this! I actually hearted it back in the day but forgot about it. Any mods reading please feel free to lock this thread to direct people to that one instead.
