[swift-evolution] What about a VBA style with Statement?
Radosław Pietruszewski
radexpl at gmail.com
Wed Apr 13 10:09:37 CDT 2016
> On 13 Apr 2016, at 16:47, Vladimir.S <svabox at gmail.com> wrote:
>
> Than you for sharing this method. So, do you suggest to not have this "with" construction in Swift just because we *can* implement the same behavior by using some workarounds/hacks?
>
> I.e. do you support this proposal or don't?
> Do you agree that such built-in feature will be very useful and make our programming live a little better/easier?
I’m -1, at least in the foreseeable future. I do agree that this is a useful construct, but if I can do it in library code, paying only a small price for this, I’d prefer Swift to grow better in places that a library *can’t* fix.
>
> I strongly believe that if such feature is really useful and handy, if it is explicit and if it is clearly showing in code what we are doing - we most likely want to have this feature as part of language rather than possibility to use some workaround to implement it.
>
> With your suggestion, how to deal with two separate classes that have no common protocol? I.e.
> class A { .. }; class B {..};
> Should we conform them to some protocol or create extension of each just to have this "with" feature? Don't think so.
No — for now, it’s best to use a free function for now. And, like I mentioned, universal conformances could allow this to be easily added to all types — and so I’d focus on pushing *that* proposal.
>
> On 13.04.2016 17:17, Radosław Pietruszewski via swift-evolution wrote:
>> It can be (more-or-less) solved in library code today:
>>
>> extension NSObjectProtocol {
>> public func with(@noescape fn: Self -> Void) -> Self {
>> fn(self)
>> return self
>> }
>> }
>>
>>
>> This way, you can do, on NSObjects:
>>
>> textLabel.with {
>>
>> $0.textAlignment = .Left
>>
>> $0.textColor = .darkTextColor()
>>
>> }
>>
>>
>> I love this pattern.
>>
>> You can also make it a function to make it work with any value of any kind
>> (it will then take form of `with(foo) { …}`).
>>
>> Ideally, if you could write a universal extension (something like
>> `extension Any`), you could just add this behavior, with method syntax, to
>> everything.
>>
>> — Radek
>>
>>> On 13 Apr 2016, at 15:15, 李海珍 via swift-evolution
>>> <swift-evolution at swift.org <mailto:swift-evolution at swift.org> <mailto:swift-evolution at swift.org <mailto:swift-evolution at swift.org>>> wrote:
>>>
>>> I recently learned some VBA and I found a very conveniently `with` statement.
>>>
>>> `with` statement can be helpful to set property for UIKit instance.
>>>
>>> for instance a UILabel instance `textLabel` ,with `with` statement we can
>>> set UILabel property like this
>>>
>>>
>>> ```swift
>>>
>>> with textLabel{
>>>
>>> .textAlignment= .Left
>>>
>>> .textColor= UIColor.darkTextColor()
>>>
>>> .font= UIFont.systemFontOfSize(15)
>>>
>>> }
>>>
>>> ```
>>>
>>> _______________________________________________
>>> swift-evolution mailing list
>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org> <mailto: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>
>>
>>
>>
>> _______________________________________________
>> 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/20160413/6033a31c/attachment.html>
More information about the swift-evolution
mailing list