# Problem fixing error message related to optionals

**URL:** <https://forums.swift.org/t/problem-fixing-error-message-related-to-optionals/63789>\
**Category:** Using Swift\
**Tags:** generics, optional, error\
**Created:** [March 18, 2023, 6:52am UTC](https://forums.swift.org/t/problem-fixing-error-message-related-to-optionals/63789 "2023-03-18T06:52:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bartvk](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bartvk/32/29197_2.png) [@bartvk](https://forums.swift.org/u/bartvk)\
**Post date:** [March 18, 2023, 6:52am UTC](https://forums.swift.org/t/problem-fixing-error-message-related-to-optionals/63789/1 "2023-03-18T06:52:49Z")

</div>

I've got some code that's generated (by SwiftGraphQL). I'd like to use it, but I'm bumping into an error message. I've tried to minimize the code; the following can be pasted into a playground:

```
public enum Objects {
    public struct Query {}
    public struct Starship {}
}

public final class Fields<TypeLock> {
    public private(set) var data: Codable

    public init(data: Codable) {
        self.data = data
    }
}

public struct Selection<T, TypeLock> {
    private var decoder: (Fields<TypeLock>) throws -> T

    public init(decoder: @escaping (Fields<TypeLock>) throws -> T) {
        self.decoder = decoder
    }
}

extension Selection where T == Never, TypeLock == Never {
    public typealias Root<T> = Selection<T, Objects.Query>
}

extension Selection where T == Never, TypeLock == Never {
    public typealias Starship<T> = Selection<T, Objects.Starship>
}

extension Fields where TypeLock == Objects.Starship {
    public func name() throws -> String? {
        return "X-Wing"
    }
}

extension Fields where TypeLock == Objects.Query {
    public func starship<T>(
        id: String,
        selection: Selection<T, Objects.Starship?>
    ) throws -> T {
        return Objects.Starship() as! T
    }
}

```

And this is my own code:

```
struct MyStarshipModel {
    var name: String

    init?(name: String?) {
        guard let name else {
            return nil
        }
        self.name = name
    }
}

let starshipSelection = Selection.Starship(decoder: {
    MyStarshipModel(name: try $0.name())
})

let query = Selection.Root {
    $0.starship(id: "abcxyz", selection: starshipSelection)
}

```

The last stanza gives me the following error:

```
Cannot convert value of type 'Selection<MyStarshipModel?, Objects.Starship>' to expected argument type 'Selection<MyStarshipModel?, Objects.Starship?>'

```

I can't for the life of me, cast the starshipSelection such that it compiles. The difference is just one optional so it shouldn't be so hard, I'm hoping I'm just tired and overlook the obvious. What's the solution here?

---

<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:** [March 18, 2023, 5:33pm UTC](https://forums.swift.org/t/problem-fixing-error-message-related-to-optionals/63789/2 "2023-03-18T17:33:42Z")

</div>

If you change "Objects.Starship?" to "Objects.Starship" and then add the missing try before "$0.starship..." - it compiles. No idea how to fix it without modifying the autogenerated source.

---

<div class="post-metadata">

**Author:** ![bartvk](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bartvk/32/29197_2.png) [@bartvk](https://forums.swift.org/u/bartvk)\
**Post date:** [March 19, 2023, 5:49pm UTC](https://forums.swift.org/t/problem-fixing-error-message-related-to-optionals/63789/3 "2023-03-19T17:49:57Z")

</div>

I've tried the following:

```
let starshipSelection: Selection<MyStarshipModel?, Objects.Starship?> =
    Selection.Starship(decoder: {
        MyStarshipModel(name: try $0.name())
    })

```

The compiler then says: `Cannot assign value of type 'Selection<MyStarshipModel?, Objects.Starship>' to type 'Selection<MyStarshipModel?, Objects.Starship?>'`

Am I not overlooking something here? It should be possible to make the `Objcts.Starship` on the right-hand-side of the assignment into an optional?
