[swift-evolution] The bind thread

Taras Zakharko taras.zakharko at uzh.ch
Mon Feb 1 19:36:45 CST 2016


I do not see any necessity in a bind keyword and I don’t believe that it adds any clarity to the language. Furthermore, the current syntax allows one to change the variable name or add additional conditions in  transparent manner. I also like that the current syntax make it very obvious that a variable is declared within a local scope. Flow-typing already exists in Swift (via as?) and while it might be a bit more verbose, its also more flexible. 

However, I do agree that the specific semantic of let and var in the if context is a bit unexpected. So maybe one should look into making the optional unwrapping semantics more explicit. E.g. what about changing the semantics of ! so that it can be safely used in a conditional context? E.g.

if let x = x! {

}

if let x = x as! SomeType {

}

This would make it very clear that the value is being unwrapped without adding a lot of clutter or additional effort. 

Alternative options for making binding more explicit

if let x ?= x 
if let x ? x
if let? x = x

etc. But I do like the ! refinement above. 

— Taras

> On 02 Feb 2016, at 01:49, Andrew Bennett via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I mostly agree with everything you said. I also think "if case" syntax is bad at the moment.
> 
> However I think any changes probably have to be an addition rather than a replacement.
> 
> I've tried to reconcile these changes with pattern matching and cannot work out how it fits. There's too many inconsistencies and incompatibilities. At best you can make a new pattern matching syntax that's incompatible with switch statements.
> 
> On Tuesday, 2 February 2016, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> >   if bind foo {
> >       // foo is non-optional in here
> >   }
> 
> If something in a flow control statement should read like an assertion, we don't want "bind" here.
> 
>         if has foo {
>                 // foo is non-optional
>         }
> 
> However, I do note that pattern matching in an if statement already lets you do the same thing as `if let` more explicitly:
> 
>         if case let foo? = bar {
> 
>         }
> 
> I wonder if we should instead enhance this feature's usability. For instance, as a surgical change, we could drop the `case` keyword when `let` is present in the condition (since `let` isn't allowed in a boolean expression anyway):
> 
>         if let foo? = bar {
> 
>         }
> 
> This is one character longer than the current `if let`, but it falls naturally out of other language features and allows for other similar constructs, like `if let .Success(value) = bar`.
> 
> However, the `if case` syntax kind of sucks in other ways, too, and I think it deserves another look. In particular, the `=` isn't great for pattern matching that doesn't bind variables; people have complained about that part of `if case` before. Maybe we can improve that while still making the unwrapping good by introducing a keyword or operator that replaces the `=` while implying the `case` so it doesn't have to be stated explicitly. Here's a strawman of what this would look like, using `matches`:
> 
>         if let foo? matches bar
>         if let .Success(value) matches bar
>         if .Card(let rank, .Heart) matches bar
>         if .OK matches bar
> 
> Can we do this with `for case` too? Maybe...
> 
>         for let foo? matches in bars
>         for let .Success(value) matches in bars
>         for .Card(let rank, .Heart) matches in bars
>         for .OK matches in bars
> 
> I think this approach might work; the only question is what `matches` should be. I don't like using a keyword for it; I think it disappears too easily, particularly in the optional case. We do sort of have a pattern matching operator, `~=`, but it's very obscure, the overloading is not really right, and I can never remember which order the characters go in (possibly because Perl uses `=~` as its matching operator). Colon kind of reads well, but it disappears too easily, and it's already associated with typing:
> 
>         if let foo?: bar
>         if let .Success(value): bar
>         if .Card(let rank, .Heart): bar
>         if .OK: bar
> 
> I don't know what the answer is here, but I think this might be a good line to pursue.
> 
> --
> Brent Royal-Gordon
> Architechies
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160202/fd181aa3/attachment-0001.html>


More information about the swift-evolution mailing list