# How does Swift allow assignment of an optional type to a non optional type?

**URL:** <https://forums.swift.org/t/how-does-swift-allow-assignment-of-an-optional-type-to-a-non-optional-type/40100>\
**Category:** Using Swift\
**Created:** [September 8, 2020, 2:40am UTC](https://forums.swift.org/t/how-does-swift-allow-assignment-of-an-optional-type-to-a-non-optional-type/40100 "2020-09-08T02:40:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dragon\_Hatcher](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@Dragon\_Hatcher](https://forums.swift.org/u/Dragon_Hatcher)\
**Post date:** [September 8, 2020, 2:40am UTC](https://forums.swift.org/t/how-does-swift-allow-assignment-of-an-optional-type-to-a-non-optional-type/40100/1 "2020-09-08T02:40:51Z")

</div>

For example, this works:  
`var x: Int? = 3`

I know that optionals are just an enum with an associated value so I was wondering if this is something that is achievable by normals users or if this is compiler magic.

---

<div class="post-metadata">

**Author:** ![AlexanderM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexanderm/32/9781_2.png) [@AlexanderM](https://forums.swift.org/u/AlexanderM)\
**Post date:** [September 8, 2020, 2:56am UTC](https://forums.swift.org/t/how-does-swift-allow-assignment-of-an-optional-type-to-a-non-optional-type/40100/2 "2020-09-08T02:56:12Z")

</div>

It's compiler magic, non-optional values can be automatically promoted to optionals in certain contexts.

You can narrowly achieve the same thing for literals (like `nil`, `123`, `123.45`, `true`, `"string"`, `["array"]`, `["dict": 123]`, etc,) using the [`ExpresisbleByXLiteral` protocols](https://developer.apple.com/documentation/swift/swift_standard_library/initialization_with_literals).

Swift doesn't have anything like C#'s [user defined conversion operators](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/user-defined-conversion-operators) that would allow you to implement implicit conversion like this in the general case (for non-literal values).

I have mixed feelings. There are certainly times when they're really cool and clean up noisy code. But it also makes it harder to read code if you're not super attentive and don't have access to an IDE to assist your navigation (e.g. you're reading code on GitHub in your browser).

---

<div class="post-metadata">

**Author:** ![Dmitriy\_Ignatyev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dmitriy_ignatyev/32/11340_2.png) [@Dmitriy\_Ignatyev](https://forums.swift.org/u/Dmitriy_Ignatyev)\
**Post date:** [July 30, 2025, 3:31pm UTC](https://forums.swift.org/t/how-does-swift-allow-assignment-of-an-optional-type-to-a-non-optional-type/40100/3 "2025-07-30T15:31:51Z")

</div>

It is not only about conversion operator. Swift compiler treat T as a subtype of `Optional<T>`.  
Unfortunately it seems there is no way to do the same trick for custom types.

```swift
enum CopyingChoice<NewValue> {
    case copyFromSource
    case new(NewValue)
    case removeCurrent
}

struct Foo {
  // properties ...

  func copy(name: CopyingChoice<String> = .copyFromSource,
            price: CopyingChoice<Double> = .copyFromSource) -> Self {
    ...
  }
}

```

What I want is: `product.copy(price: 99.9) // 99.9 can not be assigned to argument of type CopyingChoice<Double>`  
So the only way is: `product.copy(price: .new(99.9))`

It will be very good to have a feature for explaining compiler how to init such types from their Wrapped types.  
It will be useful for not only enums, but for structs too (if they have special initializers in a manner of `ExpressibleBy` protocols family).

Another direction is Optional-like syntax for other types like Result, Either and others:

```swift
var optional: Int?
optional = nil // we don't write .none here
optional = 5 // we don't write .some(5) here

```

So this way it should be nice to write:

```swift
var result: Result<Int, DecodingError>
result = 5 // instead of result = .success(5)
result = DecodingError() // instead of result = .failure(DecodingError())

```

Of course there should be reasonable limitations to avoid pitfalls. As an example, this should be prohibited:

```swift
public enum Either<L, R> {
  case left(L)
  case right(R)
}

var foo: OneOf2<String, any CustomStringConvertible>
foo = 5 // ok, here it is clear that is .second(5)
foo = "Hello" // can be assigned to both cases, no way to choose one

// even worse if `String` type for .left will be change to `any CustomStringConvertible` later

```

---

<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:** [July 30, 2025, 10:14pm UTC](https://forums.swift.org/t/how-does-swift-allow-assignment-of-an-optional-type-to-a-non-optional-type/40100/4 "2025-07-30T22:14:24Z")

</div>

> [@Dmitriy\_Ignatyev](#):
>
> `optional = nil // we don't write .none here`

This we could do today by adopting `ExpressibleByNilLiteral`.

> [@Dmitriy\_Ignatyev](#):
>
> ```swift
> optional = 5 // we don't write .some(5) here
> 
> ```

For this we'd need something like:

```swift
protocol ExpressibleByWrappedValue {
    associatedtype Wrapped
    init(_ wrapped: Wrapped)
}

```

Until then consider some ad-hoc workaround:

```swift
enum MyOptional<Wrapped> {
    case none
    case some(Wrapped)
}

prefix operator £

prefix func £<Wrapped>(_ wrapped: Wrapped) -> MyOptional<Wrapped> {
    .some(wrapped)
}

var value = 42
var opt: MyOptional<Int> = £value

```

I often experiment with such things but then realise the total saving is not significant, YMMV.
