Cenny
Cenny

Reputation: 1984

Generic typealias with Void gives error missing argument in Swift 4

When I tried to migrate to Swift 4 an error occurred with my generic typealias for closures if you give it Void as an input. This worked fine in Swift 3 but not so much now, anybody know why, and is this a bug or a feature?

There is a workaround by defying another closure that explicit handles this case. But would be nice to not have to use this workaround and to understand why this error arose in Swift 4.

typealias Handler<T> = (T) -> Void

func foo(completion: Handler<String>) {
    completion("fooing")
}

// This worked in Swift 3, but not in Swift 4
func bar(completion: Handler<Void>) {
    completion() // Missing argument for parameter #1 in call
}

Upvotes: 3

Views: 1865

Answers (1)

Cenny
Cenny

Reputation: 1984

It seems that this is not a bug but how Swift works now. You cannot omit an associated value of type Void anymore.

I have found two passable workarounds:

The first one just explicitly just pass the value.

func bar(completion: Handler<Void>) {
    completion(())
}

The second solution is to declare another typealias for void cases.

typealias VoidHandler = () -> Void

func barbar(completion: VoidHandler) {
    completion()
}

Upvotes: 8

Related Questions