Skip to content

Result initializer works in for loop but not map? #273

@getaaron

Description

@getaaron

This code compiles without issue, where generatedEvents is of type [EKEvent] and eventStore is an EKEventStore:

for thisEvent in generatedEvents {
    let result = Result(try eventStore.save(thisEvent, span: .thisEvent, commit: false))
    print(result)
}

However, when I rewrite it as a map (to generate an array of Results):

let results = generatedEvents.map { thisEvent -> Result<(), AnyError> in
    let result = Result(try eventStore.save(thisEvent, span: .thisEvent, commit: false))
    print(result)
    return result
}

I receive the compiler error:

Call can throw, but it is not marked with 'try' and the error is not handled

However, looking at the Result initializer, it looks like the error is handled in a do/catch block.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions