Question: How to check/predicate function type in Swift?

Question

How to check/predicate function type in Swift?

Answers 3
Added at 2016-12-31 16:12
Tags
Question

For example:

func f(x: Int) -> Int {
    return x
}


func h(f: @escaping (Int) -> Any) {
    if (f is (Int) -> Int) {
        print(f(1))
    } else {
        print("invalid")
    }
}


h(f: f)

I expect it to print out 1 but it actually prints out invalid.

Answers
nr: #1 dodano: 2016-12-31 16:12

There's a workaround using generics:

func intF(x: Int) -> Int {
    return x
}

func stringF(x: Int) -> String {
    return "\(x)"
}

func h<T>(f: (Int) -> T) {
    if (T.self == Int.self) {
        print(f(1))
    } else {
        print("invalid")
    }
}

h(f: intF)    // prints: 1
h(f: stringF) // prints: invalid
nr: #2 dodano: 2016-12-31 19:12

Using Any is almost always a sign of code smell, you should try to rely as much as possible of the type safety that Swift provides. You can achieve this by making h generic, thus verifiable at compile time.

// the overload that does the actual stuff
func h(f: @escaping (Int) -> Int) {
    print(f(1))
}

// this maps to all other types
func h<T>(f: @escaping (Int) -> T) {
    print("invalid")
}

h { _ in return "15" }  // Invalid
h {  2 * $0 }           // 2

Heck, you could even give up the generic overload, thus you'll have for free compile checks instead of runtime failures (much, much reliable and predictive)

nr: #3 dodano: 2017-01-01 06:01

You can rewrite h into a generic function:

func h<T>(f: @escaping (Int) -> T) {        
    if T.self == Int.self  {
        print(f(1))
    } else {
        print("invalid")
    }
}

But the better way to to write type-specific overloads for h and a generic catch-all for the rest (if you need it at all).

Source Show
◀ Wstecz