[swift-evolution] Property Getter Return Statement

Slava Pestov spestov at apple.com
Tue Oct 10 15:02:37 CDT 2017


> On Oct 10, 2017, at 1:00 PM, Slava Pestov <spestov at apple.com> wrote:
> 
>> 
>> On Oct 9, 2017, at 10:54 AM, Jordan Rose <jordan_rose at apple.com> wrote:
>> 
>> 
>> 
>>> On Oct 8, 2017, at 21:56, Slava Pestov via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>> 
>>> 
>>>> On Oct 7, 2017, at 7:07 AM, James Valaitis via swift-evolution <swift-evolution at swift.org> wrote:
>>>> 
>>>> Is it widely agreed that it is necessary to require a return statement on a one line property getter?
>>>> 
>>>> var session: AVCaptureSession { get { return layer.session } }
>>>> 
>>>> Or could we follow the convention for any other close and get rid of it? For me it seems redundant; the word `get` literally precedes the closure.
>>> 
>>> In multi-file projects, re-compiling one file that references the property would necessitate type checking the body of the getter, even if the getter is defined in a different source file. So one reason not to have this would be to avoid slowing down type checking.
>> 
>> This is not correct. Omitting the "return" is different from omitting the property's type.
>> 
>> (I'm minorly in favor of allowing the 'return' to be omitted for single-expression getters. Not enough to be the person who implements it, but enough to +1 a proposal-with-implementation even in the Swift 5 timeframe.)
> 
> I’m minorly opposed, because it feels like a slippery slope. What about function bodies? etc
> 
> func foo() -> Int { 3 } // should this be allowed?

In fact the reason I thought the original thread was about omitting the return type, rather than just omitting the ‘return’, is that I feel the main reason that single-expression closures exist is so that their type can be inferred as part of the expression containing the closure. Swift does not infer types across statement boundaries, so single expression closures exist, as a special case, to help you avoid declaring types in some cases.

Chris will say no, it’s about concision, and omitting the ‘return’, but deep down in his heart, he knows that single-expression closures were really just a type inference hack :-)

Slava

> 
> Slava
> 
>> 
>> Jordan

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


More information about the swift-evolution mailing list