[swift-evolution] Computed properties: get and set using shorthand notation?

Howard Lovatt howard.lovatt at gmail.com
Wed Feb 3 16:29:39 CST 2016


You can keep going on this line of thinking, e.g.:

    let posC = { max(0, $0) }

Is legal but you have to write:

    func posF(x: Int) -> Int { return max(0, x) }

On Thursday, 4 February 2016, Chris Liscio via swift-evolution <
swift-evolution at swift.org> wrote:

> Why can’t we use shorthand argument names and implicit returns when
> specifying the get and set blocks in a computed property? I.e., Why must we
> refer to newValue in the implementation of set, and not $0 like we do in
> closures? Also, why do we need ‘return’?
>
> Once you get used to the shorthand, you kinda want to use it all over…
>
> This question came up recently when I was writing a façade for another
> class, and would have liked to written something like the following:
>
> class Façade {
>   private var _wrapped: Something
>
>   var wrappedProperty: Int {
>     get { _wrapped.property }
>     set { _wrapped.property = $0 }
>   }
>
>   var multipliedProperty: Int { _wrapped.property * 42 }
> }
>
> I apologize if this has already been discussed/requested before. I
> searched the archives and didn’t turn up anything that referred to this
> specifically.
>
> Thanks!
>
> Chris Liscio
> http://supermegaultragroovy.com
> Learn _your_ music with Capo: http://capoapp.com
>
>

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


More information about the swift-evolution mailing list