[Pitch] Last expression as return value

The reason for rejecting the then statement is mentioned at the beginning of the proposal. However, one advantage of the then statement that wasn’t discussed is its ability to allow early exits. As shown below, early exits can be implemented conveniently, which is why I prefer the then statement.

func handle(post: Post) -> Result<Entity, Err> {
    var errors = Err()

    let empty = Entity.empty

    let entity = switch validate(post: post) {
    case .failure(let e):
        errors.append(e)
        return .failure(errors)
    case .success(let x):
        if x.isEmpty {
            // We can make an early exit like this when using `then`
            then empty
        }

        logger.info("ok \(x)")
        then x
    }

    entity.store()

    return .success(entity)
}
4 Likes