Search code examples
swiftreduceenumeration

Swift reduce - why is value optional?


Why is total optional on line return total + 1?

return first.enumerated().reduce(0) { total, letter in
   let index = first.index(first.startIndex, offsetBy: letter.offset)
   if first[index] != second[index]{
       return total + 1
   }
   return total
}

Value of optional type 'Int?' must be unwrapped to a value of type'Int' Coalesce using '??' to provide a default when the optional value contains 'nil' Force-unwrap using '!' to abort execution if the optional value contains 'nil'

So this fixes it:

return first.enumerated().reduce(0) { total, letter in
   let index = first.index(first.startIndex, offsetBy: letter.offset)
   if first[index] != second[index]{
       return total! + 1
   }
   return total
}

If I break it down the change happens on adding let index....

OK - This returns the total count of first and total is not optional:

return first.reduce(0) { total, letter in
    return total + 1
}

OK - This enumerated and total is not optional:

return first.enumerated().reduce(0) { total, letter in
    return total + 1
}

ERROR - This gets a compile error that total is optional

return first.enumerated().reduce(0) { total, letter in
    let index = first.index(first.startIndex, offsetBy: letter.offset)
    return total + 1
}

Solution

  • In order for you to get this result at all (as far as I can tell), the enclosing function must return an Int?. The implication is that reduce can return an optional. Absent the conditional, the compiler can determine that reduce will never return nil, i.e., total is never nil. So, the compiler infers that the return type of the closure is Int. The compiler appears to be entangling type inferencing for the reduce closure and total. Once you add the conditional, the compiler is incapable of determining whether the reduce will return nil or not. Now when it unnecessarily infers the type for total it gets it wrong.

    To me this looks like a case of Swift type inferencing gone astray. Clearly, total is never nil based on the documentation of enumerated.

    If you modify the code slightly you get the expected result:

       return first.enumerated().reduce(0) { (total: Int, letter) in
           let index = first.index(first.startIndex, offsetBy: letter.offset)
           if first[index] != second[index]{
              return total + 1
           }
           return total
       }
    

    Swift makes a lot of type inferences and it is really great because I get strong typing while retaining many of the benefits of a dynamic language. In my experience, however, swift's inferences can be mystifying sometimes. It handles arcane situations with ease and stumbles over something I think is obvious.

    It looks like a bug to me.