[swift-evolution] Modernize Switch/Case Statements?

Radosław Pietruszewski radexpl at gmail.com
Sun Jan 31 13:02:21 CST 2016


> 
> // this is how I have to do things now
> 
> let _ : UILabel = {
>     view.addSubview($0)
>     CenterViewInSuperview($0,
>         horizontal: true, vertical: false)
>     $0.text = "Toggle me"
>     $0.font = UIFont.boldSystemFontOfSize(36)
>     ConstrainViews("V:[view1]-30-[view2]",
>         views: $0, mySwitch)
>     return $0
> }(UILabel())
> 
> // vs (this does not exist in the language)
> 
> do {
>     view.addSubview($0)
>     CenterViewInSuperview($0,
>         horizontal: true, vertical: false)
>     $0.text = "Toggle me"
>     $0.font = UIFont.boldSystemFontOfSize(36)
>     ConstrainViews("V:[view1]-30-[view2]",
>         views: $0, mySwitch)
> }(UILabel())


That’s an interesting pattern. I don’t think I’ve seen that one before.

How about:

do {
    let v = UILabel()
    view.addSubview(v)
    CenterViewInSuperview(v,
        horizontal: true, vertical: false)
    v.text = "Toggle me"
    v.font = UIFont.boldSystemFontOfSize(36)
    ConstrainViews("V:[view1]-30-[view2]",
        views: v, mySwitch)
}


Is it too bad? I certainly like it better because you introduce `v` ($0) at the beginning of the block, not at the end.

And while one letter variable name is kinda gross in general, I don’t mind it in such context just like I don’t have a problem with $0 in simple closures. It’s a common practice in Ruby, for example, that doesn’t have $x to define “obvious” closure arguments as one letter variables, like so:

   articles = article_data.map { |a| Article.new(a) }

— Radek

> On 31 Jan 2016, at 18:27, Erica Sadun via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
>> On Jan 31, 2016, at 1:34 AM, Thorsten Seitz via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> 
>> 
>>> Am 30.01.2016 um 10:39 schrieb Haravikk via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>>:
>>> 
>>> Actually, one thing we don’t have in Swift is the ability to just put blocks (curly braces) wherever we like, which in some languages is a useful tool for variable scope when you know you only need something for a short time, but might want to re-use the name.
>> 
>> You can use a "do" block for that.
>> 
>> do { ... }
>> 
>> -Thorsten 
> 
> do blocks don't let you introduce parameters for short-lived items:
> 
> 
> // this is how I have to do things now
> 
> let _ : UILabel = {
>     view.addSubview($0)
>     CenterViewInSuperview($0,
>         horizontal: true, vertical: false)
>     $0.text = "Toggle me"
>     $0.font = UIFont.boldSystemFontOfSize(36)
>     ConstrainViews("V:[view1]-30-[view2]",
>         views: $0, mySwitch)
>     return $0
> }(UILabel())
> 
> // vs (this does not exist in the language)
> 
> do {
>     view.addSubview($0)
>     CenterViewInSuperview($0,
>         horizontal: true, vertical: false)
>     $0.text = "Toggle me"
>     $0.font = UIFont.boldSystemFontOfSize(36)
>     ConstrainViews("V:[view1]-30-[view2]",
>         views: $0, mySwitch)
> }(UILabel())
> 
> -- E
> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

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


More information about the swift-evolution mailing list