Skip to content

Optionally bind elements before appending to Arrays #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2016
Merged

Optionally bind elements before appending to Arrays #170

merged 1 commit into from
Mar 3, 2016

Conversation

ArtSabintsev
Copy link
Contributor

This is a bit safer than the force-unwrap solution.

This is a bit safer than the force-unwrap solution.
if let e = e as? T {
t.append(e)
} else if let e = e as? U {
u.append(e)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about

if case let e as T = e {
    t.append(e)
} else if case let e as U = e {
    u.append(e)
}

Does the same thing but avoids the ? in as

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure - that works!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as for me if case let is to heavy

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, that does not work at all. Still asks to force-downcast. Agree with @kostiakoval.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me

extension Array {
    public func partition<T, U>() -> ([T], [U]) {
        var t = [T]()
        var u = [U]()
        for e in self {
            if case let e as T = e {
                t.append(e)
            } else if case let e as U = e {
                u.append(e)
            }
        }
        return (t, u)
    }
}

let a = [1, 2, 3, "2", "3"]
let partition: ([Int], [String]) = a.partition()

agreed though that if case let looks heavier at first but I have grown to like it lol

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't say better or worse just different syntactically I guess?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aciidb0mb3r's solution is syntactic sugar (https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/optional-pattern). Same solution, different way of implementing, which I would deem non-traditional.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both solutions works fine and there is no big difference. if let e = e as? T solutions is safe, very clear and it's very often used in Swift community.
I feel there is no need for further discussion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 👍

kostiakoval added a commit that referenced this pull request Mar 3, 2016
…nsions

Optionally bind elements before appending to Arrays
@kostiakoval kostiakoval merged commit 8cbdaef into swiftlang:master Mar 3, 2016
aciidgh pushed a commit to aciidgh/swift-package-manager that referenced this pull request Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants