Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
}
like image 702
Cenny Avatar asked Dec 18 '25 20:12

Cenny


1 Answers

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()
}
like image 68
Cenny Avatar answered Dec 21 '25 10:12

Cenny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!