[swift-users] Optional variables from a non-optional source with type inference

Michael Henson mikehenson at gmail.com
Mon Jan 25 18:16:30 CST 2016


While reading the summary of a newsletter's notification about the
acceptance of SE-0021's new syntax, a thought occurred to me:

let fn = someView.insertSubview(_:at:)
> let fn1 = someView.insertSubview(_:aboveSubview:)
> let fn2 = someView.insertSubview(_:belowSubview:)


That code will create variables fn, fn1, fn2 of the correct type to hold
each of the named functions.

As I understand it, the following things are true:

1. Member functions on instances of a class type are curried functions that
strongly capture 'self'.
2. Any strong reference to those member functions will also prevent that
instance from ever reaching ref count 0.
3. This isn't a new problem, just one that will become more important now
that this use case is easier to... use.

The easy fix for this situation would be to declare fn, fn1, and fn2 as
weakened optionals so they don't keep someView around if it should have
been dallocated. The question is... how? Is there existing syntax that
means "infer the type from the right-hand-side production, except as an
optional / weakened optional"?

Mike
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160125/b26e6b5e/attachment.html>


More information about the swift-users mailing list