Skip to content

[SR-1976] Closure signature in Swift 3 required for inout params #44585

@swift-ci

Description

@swift-ci
Previous ID SR-1976
Radar None
Original Reporter erica (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug
Assignee erica (JIRA)
Priority Medium

md5: 5e016df9563a1b6894b80711441939f1

is duplicated by:

  • SR-3632 Swift is unable to properly infer the type of closures with inout parameters without specific type information.

relates to:

  • SR-3520 Generic function taking closure with inout parameter can result in a variety of compiler errors or EXC_BAD_ACCESS

Issue Description:

For whatever reason, you have to declare the closure signature with inout variables in Swift 3. You didn't in Swift 2. Example below. Also affected the new stdlib sequence functions.

Swift 2:

func myFor<T>(args: T, _ test: (T) -> Bool, _ next: (inout T) -> Void, body: (inout T) -> Void) {
    var state = args
    while test(state) {
        defer { next(&state) } ; body(&state)
    }
}

myFor(0, { $0 < 10 }, { $0 += 2 }) {
    print($0)
}

Swift 3:

func myFor<T>(_ args: T, _ test: (T) -> Bool, _ next: (inout T) -> Void, body: (inout T) -> Void) {
    var state = args
    while test(state) {
        defer { next(&state) } ; body(&state)
    }
}

myFor(0, { $0 < 10 }, { (i: inout Int) in  return i += 2 }) {
    print($0)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itself

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions