[swift-evolution] What about a VBA style with Statement?

Vladimir.S svabox at gmail.com
Thu Apr 14 08:49:12 CDT 2016


Agree, that we can already use some hacks(IMO) to have some kind of what we 
need.
Actually, we already discussed some variants : method "with" in extension 
to type and global generic function "with". Each of them has disadvantages 
in compare to "with" language construction. In your example you need to 
create temp instance, need "return", calls the block() - many noise for 
simple action.

Also, such workarounds not always protect us from problems(like modifying 
constant struct instance in generic "with" method - it compiles. but raise 
runtime error) and each of us needs to move these workarounds from project 
to project, plus we use different implementation for the "with" feature, no 
standard, in one project with many developers we'll find different variants 
for the same "with" feature.

I believe we need such language construction in some or another implementation.

I suggest these constructions:

// similar to "if let.. " and "guard let.."
with let questionLabel = UILabel() {
   //...
}

with var some = SomeStruct() {
   //...
}

with questionLabel {
   // ..
}

And suggest to discuss these variants:

"one-point" (my preffered) :

with questionLabel {
   .textAlignment = .Center
   .font = UIFont(name:"DnealianManuscript", size: 72)
   .text = "?"
   .numberOfLines = 0
}

"two points":

with questionLabel {
   ..textAlignment = .Center
   ..font = UIFont(name:"DnealianManuscript", size: 72)
   ..text = "?"
   ..numberOfLines = 0
}

"$" sign :

with questionLabel {
   $.textAlignment = .Center
   $.font = UIFont(name:"DnealianManuscript", size: 72)
   $.text = "?"
   $.numberOfLines = 0
}

"$0" (don't think this is good, as there can not be $1 etc):

with questionLabel {
   $0.textAlignment = .Center
   $0.font = UIFont(name:"DnealianManuscript", size: 72)
   $0.text = "?"
   $0.numberOfLines = 0
}

Opinions?

On 14.04.2016 13:41, Milos Rankovic via swift-evolution wrote:
>> On 13 Apr 2016, at 17:04, Erica Sadun via swift-evolution
>> <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>
>> The biggest advantage of thewithpattern IMO is Cocoa initializers. It
>> provides a more unified
>> initialization scope. Instead of:
>>
>> letquestionLabel =UILabel()
>> questionLabel.textAlignment= .Center
>> questionLabel.font= UIFont(name:"DnealianManuscript", size:72)
>> questionLabel.text=currentQuestion.text
>> questionLabel.numberOfLines=0
>
> Whilst I would not mind the `with` pattern, it’s worth noting that Swift
> already has some very fine alternatives:
>
> letquestionLabel: UILabel= {
> let$ = UILabel()
> $.textAlignment= .Center
> $.font= UIFont(name:"DnealianManuscript", size: 72)
> $.text= "?"
> $.numberOfLines= 0
> return$
> }()
>
>
> Including some lazy options:
>
>
> private(set)lazyvarquestionLabel: UILabel= {...}()
>
>
> staticletquestionLabel: UILabel= {...}()
>
> milos
>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>


More information about the swift-evolution mailing list