[swift-evolution] Reconsidering SE-0003 Removing var from Function Parameters and Pattern Matching
Thorsten Seitz
tseitz42 at icloud.com
Sat Jan 30 10:27:03 CST 2016
> Am 29.01.2016 um 20:11 schrieb Jordan Rose via swift-evolution <swift-evolution at swift.org>:
>
>>
>> On Jan 29, 2016, at 8:54, Dave Abrahams via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>
>>
>> on Thu Jan 28 2016, Thorsten Seitz <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>
>>> This is called flow typing in Ceylon and they are using "exists" for
>>> that case but testing for conformance with "is" works just the same
>>> way there. The latter is of importance because the common use of union
>>> types in Ceylon.
>>>
>>> if exists foo {
>>> // foo is non-optional here
>>> }
>>>
>>> if foo is String {
>>> // foo is of type String here
>>> }
>>
>> FWIW, some of us are concerned that if the types of names change
>> without being announced by a let or var binding, it will be confusing.
>> That's one reason we haven't gone this way in the past.
Actually I find the common pattern `if let x = x { … }` which uses shadowing to be more confusing as it looks like a no-op.
And of course this can be circumvented in many cases by using a different name but in my experience there often is no other good name.
> It's also problematic if 'foo' is mutable, because then it could go from being non-nil to being nil. You could restrict it to constants, but then it doesn't work on instance properties.
Ceylon requires the value to be checked to be non-mutable, but it allows initializing a value within the exists expression (quite similar to `if let`):
So this would not be allowed (all code Swiftified):
var foo: Int? = …
if exists foo { // type error: foo must be a constant
…
}
But this would:
var foo: Int? = …
if exists bar = foo {
// bar has type Int
foo = nil
}
So with `exists` we could do the same as with if-let but the common case would be simpler and more elegant.
I’m only afraid that `if var` could not be replaced sensibly by flow typing and would have to be replaced by the following (like the original SE-0003 would have required), which might not be too bad because of less shadowing happening:
var foo: Int? = …
if exists foo {
var copy = foo
// mutate and use copy
}
Ceylon even makes use of `assert` in flow typing which I think is a great feature:
let foo: Int? = ...
assert(exists foo) // because I know for some reason that the compiler cannot derive that foo will be not nil
// here foo has type Int
-Thorsten
>
> Jordan
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160130/480d7f71/attachment.html>
More information about the swift-evolution
mailing list