Calling (Void) -> () with Void

Hello,

I’m having some odd results with closures that take Void as a parameter, and was wondering if this was expected behavior or a bug. Specifically, I have the following closure:

Welcome to Apple Swift version 4.0 (swiftlang-900.0.45.6 clang-900.0.26).
let foo: (Void) -> () = {}

Trying to call foo without parameters fails, of course:

foo()

error: missing argument for parameter #1 in call
foo()
    ^
    <#Void#>

However, the fix-it doesn’t seem to work:

foo(Void) // or even: foo(Void())

error: argument passed to call that takes no arguments
foo(Void)
   ^~~~~~

while

foo(()) // Executes with no errors

works. Since Void is a typealias for () this should work, shouldn’t it? Just wanted to confirm that I’m not going crazy here.

Regards,
Saagar Jha

I think the fix-it is hinting that you need a value of type Void, not
literally Void itself. It's a bit confusing since () is both a type and
a value, while Void is only a type since it's declared as a typealias.
Typealiases aren't processed like macros are, it would seem.

I think the convention for function types is to only use Void after the
arrow (if at all) to avoid this confusion:

let foo: () -> Void = {} // or just () -> (), takes no params

let bar: (()) -> () // clear(er) that it takes one param of type ()

···

On 7/9/2017 8:43 PM, Saagar Jha via swift-users wrote:

Hello,

I’m having some odd results with closures that take Void as a
parameter, and was wondering if this was expected behavior or a bug.
Specifically, I have the following closure:

    > Welcome to Apple Swift version 4.0 (swiftlang-900.0.45.6
    clang-900.0.26).
    > let foo: (Void) -> () = {}

Trying to call foo without parameters fails, of course:

    > foo()
    error: missing argument for parameter #1 in call
    foo()
        ^
        <#Void#>

However, the fix-it doesn’t seem to work:

    > foo(Void) // or even: foo(Void())
    error: argument passed to call that takes no arguments
    foo(Void)
       ^~~~~~

while

    > foo(()) // Executes with no errors

works. Since Void is a typealias for () this should work, shouldn’t
it? Just wanted to confirm that I’m not going crazy here.

Regards,
Saagar Jha

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users