[swift-evolution] Customized Inline Init Closure

Weston Catron wcatron at catrondevelopment.com
Mon Jan 4 01:03:04 CST 2016


Thanks! Then looks very interesting and I’ll reach out to the lead there and see if they’re interested in working together. It definitely highlights similar readability issues I had outlined. I’m curious if there are compiler or other advantages to initializing more properties before continuing in the scope. For example race conditions, mulitthread support, concurrency... If you want to initialize more of an object than any single initializer allows being able to create it and set all desired properties before the system denotes it as officially initialized might be helpful. This would make a distinction between the init then { // do something} model verses init with { // do this to create it }.

-Weston

> On Jan 3, 2016, at 9:09 PM, Adriano Ferreira <adriano.ferreira at me.com> wrote:
> 
> Hey there!
> 
> As a suggestion, check out this simple yet very interesting project called Then <https://github.com/devxoul/Then> by @devxoul.
> 
> Best,
> 
> — A
> 
>> On Jan 3, 2016, at 1:37 AM, Weston Catron via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> Ability to write an initializer while initializing an object.
>> 
>> Example
>> 
>> let name = “John Apple”;
>> let person = Person {
>>     self.name = nameInput.first() + " " + nameInput.last()
>>     self.dob = dateInput.datetime()
>>     If (self.age() > 18) {
>>         self.taxableStatus = INDEPENDANT
>>     } else {
>>         self.taxableStatus = DEPENDANT
>>     }
>> };
>> 
>> Helpful examples: Objects with many required parameters that are defaulted in the initializers. 
>> 
>> SKLabelNode
>> 
>> let label = SKLabelNode(text: "Example") 
>> label.position = CGPoint(x: 0, y: 250); 
>> label.fontSize = 34; 
>> label.fontColor = UIColor.blackColor() 
>> self.addChild(label);
>> 
>> Can become:
>> 
>> let label = SKLabelNode(text: self.package!.title) {
>>     self.position = CGPoint(x: 0, y: 250)
>>     self.fontSize = 34
>>     self.fontColor = UIColor.blackColor() 
>> }
>> self.addChild(label)
>> 
>> Readability Instead of a large amount of code setting up temporary variables to pass into an initializer, all initializing code could be wrapped in a closure.
>> 
>> Flexibility Instead of exhaustive initializers covering many use cases. Simpler initializers can be extended as needed. This can also encourage required properties over optional ones that are immediately defined. 
>> 
>> Compiler Warnings Closures react the same as initializers within classes, warning users of incomplete implementation of required properties.
>> 
>> Possible disadvantages:
>> 
>> Sloppy Coding Instead of writing complete initializers programmers can just rely on in-line initializers.  
>> 
>> Tried Before I found this feature is also available in C# (https://msdn.microsoft.com/en-us/library/bb397680.aspx <https://msdn.microsoft.com/en-us/library/bb397680.aspx>). Not sure if it was helpful then but many languages since don't appear to use it. 
>> 
>> -Weston
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto: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/20160104/996f7bcc/attachment.html>


More information about the swift-evolution mailing list