How can I achieve overloading both generic and non-generic function signatures for optionals in Swift 5.1 (Xcode 11 Playground)?

jgvb
import Foundation

enum Errors: Error {
    case badParse
}

public typealias JSONDictionary = [String: Any]

public func decode(_ dictionary: JSONDictionary, key: String) throws -> URL {

    guard let string = dictionary[key] as? String else {
        throw Errors.badParse
    }

    if let url = URL(string: string) {
        return url
    }

    throw Errors.badParse
}

public func decode<T>(_ dictionary: JSONDictionary, key: String) throws -> T {

    guard let value = dictionary[key] else {
        throw Errors.badParse
    }

    guard let attribute = value as? T else {
        throw Errors.badParse
    }

    return attribute
}

let url: URL = try decode(["url":"test/url"], key: "url") // url: test/url
let urlOptional: URL? = try? decode(["url":"test/url"], key: "url") // nil

The above code works as long as you're decoding a NON-optional. The specific non-generic decode function is called and the URL is constructed.

However, if you have an optionally type variable and decode it, it will not use the correct function. In swift 4.2, both optionals and non optionals would use the correct non-generic function, but since I updated to Xcode 11 swift 5.1, this behavior has been seen.

Any help would be greatly appreciated!

I would prefer not to make function signatures with optional returns such as

public func decode(_ dictionary: JSONDictionary, key: String) throws -> URL? {

because it is not scalable...and it used to work without it.

Sweeper

Evidently, how type inference works changed a little bit in Swift 5. If you just want it to use the correct overload, you just need to give it a little push:

let urlOptional: URL? = try? decode(["url":"test/url"], key: "url") as URL

by adding as URL at the end.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

In R, how can I see the signatures for which a generic method is implemented?

How can I find a generic child in a non-generic list in swift?

How can I extend a non generic interface into a generic one?

Assigning nil to generic optionals in Swift

How can I use image inside Xcode 11 playground?

How to use Guava Optionals in a non-generic context?

How can I make a generic extension in Swift?

TypeScript Generic & Function Overloading

How can I make a typesafe generic function

How can I make a function generic on an MLReader

How can I write a class that is both inheritable and generic?

How can I know both class are from same Generic class

Call generic function from non-generic function in Swift

In Swift, how do I avoid both optionals and nil object references?

How do I disambiguate this call to a generic function with a function parameter in Swift?

How to achieve a generic like this?

Calling non generic function from generic context swift

How can I write a function that will unwrap a generic property in swift assuming it is an optional type?

Overloading generic functions in iOS Swift

Can a non-generic struct implement a generic function in rust?

How can I change the locale on the Xcode Playground

TypeScript function overloading with generic types

How can I use the same generic type in multiple methods in a non-generic static class

How can I decrease verbosity when inheriting from C# generic types to non-generic types?

How can I check if a generic class instance is actually an instance of a non-generic child?

Can dynamic casting be done in a generic function instead of overloading?

Function overloading in C with _Generic when __VA_ARG__ can be empty

How do I write a generic recursive function that takes a CollectionType in Swift?

How can I initialize a generic mutable array in Swift?

TOP Ranking

  1. 1

    pump.io port in URL

  2. 2

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  5. 5

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  6. 6

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  7. 7

    mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '127.0.0.1:3306' (111 Connection refused)

  8. 8

    What's the point of declaring a const in JavaScript

  9. 9

    The prefix "andriod" for attribute "andriod:name" associated with an element type "application" is not bound?

  10. 10

    Change both state and params dynamically in ui-sref

  11. 11

    Copying a slide from one Google Slides presentation into another

  12. 12

    Grails with Oracle thick OCI driver authenticate to Oracle with wrong user

  13. 13

    Converting a class method to a property with a backing field

  14. 14

    How to use Angular2 and Typescript in Jsfiddle

  15. 15

    clojure.lang.LazySeq cannot be cast to class clojure.lang.Associative

  16. 16

    Inner Loop design for webscrapping

  17. 17

    How to set tab order for array of cluster,where cluster elements have different data types in LabVIEW?

  18. 18

    Removed zsh, but forgot to change shell back to bash, and now Ubuntu crashes (wsl)

  19. 19

    IServiceCollection does not contain a defintion for AddHttpClient

  20. 20

    What's the difference between conflict and compulsory cache miss?

  21. 21

    How to run blender on webserver?

HotTag

Archive