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

Dany St-Amant dsa.mls at icloud.com
Thu Apr 14 19:47:42 CDT 2016


> Le 14 avr. 2016 à 09:49, Vladimir.S via swift-evolution <swift-evolution at swift.org> a écrit :
> 
> 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
> }
> 

Thought with a free standing 'with' the next request to come in, could be to be able to do something like:

with source, destination {
  $1.something = $0.somethingSimilar
  $1.x = $0.bound.left
  $1.y = $0.point.top
}

So using $0,  may be a future proofing option.

Dany

> 
> 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
> _______________________________________________
> 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