Skip to content
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

Swift-like guard statement #4

Open
ngsilverman opened this issue Dec 9, 2020 · 0 comments
Open

Swift-like guard statement #4

ngsilverman opened this issue Dec 9, 2020 · 0 comments
Labels
enhancement New feature or request

Comments

@ngsilverman
Copy link
Contributor

A few people have been comparing parts of Belay to the Swift guard statement so I looked to see if there was any inspiration to be found there.

Here in its own basic form:

guard condition else {
    // statements
    // exit
}

It is very similar to:

expect.isTrue(condition) {
    // statements
    // exit
}

Belay also provides a variant that is very useful when a false condition can be handled without exiting from the parent:

expect(condition) {
    // statements
    // does not need to exit
}

And of course if one doesn't need to handle expectations globally it's just as easy to write:

if (!condition) {
    //
}

The Swift guard also supports an "optional binding declaration":

guard let constantName = someOptional else {
    // …
}

Which is very similar to:

val constantName = expect.isNotNull(someOptional) {
    //
}

One major difference: Swift support multiple bindings, Belay does not in this case.

And another: the variables assigned a value from an optional binding can be used as part of the condition:

guard let constantName = someOptional, condition(constantName) else {
    // …
}
@ngsilverman ngsilverman added the enhancement New feature or request label Dec 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant