6

I have the following struct defined.

struct Person {

    var firstName :String
    var lastName :String
    var active :Bool
}

I have created a collection of Person as shown below:

var persons :[Person] = []

for var i = 1; i<=10; i++ {

    var person = Person(firstName: "John \(i)", lastName: "Doe \(i)", active: true)
    persons.append(person)
}

and Now I am trying to change the active property to false using the code below:

let inActionPersons = persons.map { (var p) in

    p.active = false
    return p
}

But I get the following error:

Cannot invoke map with an argument list of type @noescape (Person) throws

Any ideas?

SOLUTION:

Looks like Swift can't infer types sometimes which is kinda lame! Here is the solution:

let a = persons.map { (var p) -> Person in

        p.active = false
        return p
}

THIS DOES NOT WORK:

let a = persons.map { p in

        var p1 = p
        p1.active = false
        return p1
}
trndjc
  • 11,654
  • 3
  • 38
  • 51
john doe
  • 9,220
  • 23
  • 91
  • 167

3 Answers3

7

There are exactly two cases where the Swift compiler infers the return type of a closure automatically:

  • In a "single-expression closure," i.e. the closure body consists of a single expression only (with or without explicit closure parameters).
  • If the type can be inferred from the calling context.

None of this applies in

let inActionPersons = persons.map { (var p) in
    p.active = false
    return p
}

or

let a = persons.map { p in
        var p1 = p
        p1.active = false
        return p1
}

and that's why you have to specify the return type explicitly as in Kametrixom's answer.

Example of a single-expression closure:

let inActionPersons = persons.map { p in
    Person(firstName: p.firstName, lastName: p.lastName, active: false)
}

and it would compile with (var p) in or (p : Person) in as well, so this has nothing to do with whether the closure arguments are given explicitly in parentheses or not.

And here is an example where the type is inferred from the calling context:

let a : [Person] = persons.map { p in
    var p1 = p
    p1.active = false
    return p1
}

The result of map() must be a [Person] array, so map needs a closure of type Person -> Person, and the compiler infers the return type Person automatically.

For more information, see "Inferring Type From Context" and "Implicit Returns from Single-Expression Closures" in the "Closures" chapter in the Swift book.

Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Do you have a link to those implicit type infering rules? – Kametrixom Dec 06 '15 at 10:38
  • @Kametrixom: In https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html there are sections "Inferring Type From Context" and "Implicit Returns from Single-Expression Closures". It does not state explicitly that those are the *only* cases where the type is inferred, but that's what I figured out with try and error. – But as you can see from the examples, it does not depend on *"... using the brackets for arguments ..."* – Martin R Dec 06 '15 at 10:42
  • Yeah it seems really weird though, this works: `persons.map{ return $0 }`, but `persons.map{ print(""); return $0 }` doesn't. I guess it either is a bug or just not yet supported, because this should definitely work – Kametrixom Dec 06 '15 at 10:48
  • @Kametrixom: Apparently `return ` is also treated as single-expression closure. – Martin R Dec 06 '15 at 10:50
  • True, but this rule isn't really official, the type system should really be able to infer the type, I'm surprised it doesn't because Swift's type infering is usually very strong – Kametrixom Dec 06 '15 at 10:51
6

When using the brackets for arguments so that var works, you have to put the return type as well:

let inActionPersons = persons.map { (var p) -> Person in

    p.active = false
    return p
}
Kametrixom
  • 14,673
  • 7
  • 45
  • 62
  • Even if you don't use brackets and move the p into a new variable p1 it requires to explicitly indicate the return type. Check the update on the original post. – john doe Dec 06 '15 at 03:01
  • @johndoe Yeah I realised it too just now, I think it's a bug, because `persons.map{ return $0 }` works, but `persons.map{ print(""); return $0 }` doesn't – Kametrixom Dec 06 '15 at 03:04
  • Maybe a bug in the language! Hopefully, this can be fixed in later releases! – john doe Dec 06 '15 at 03:06
  • Also, when you are using the $0 syntax how do you declare the $0 to be a var instead of the let. – john doe Dec 06 '15 at 03:10
  • @johndoe it's not possible to declare $0 as a constant because the declaration is implicit – Kametrixom Dec 06 '15 at 03:56
0

Swift 5

The accepted answer no longer works, as of Swift 5, anyway. Closures cannot have keyword arguments anymore which means that each element of the iteration must remain a constant. Therefore, to mutate structures using map, new elements must be initialized within each iteration:

let activePersons = persons.map { (p) -> Person in
    return Person(firstName: p.firstName, lastName: p.lastName, active: true)
}
trndjc
  • 11,654
  • 3
  • 38
  • 51