[swift-evolution] Idea: Properties in Failable Initializers less verbose

Gor Gyolchanyan gor.f.gyolchanyan at icloud.com
Tue Jul 25 13:09:39 CDT 2017


This was one of the issues I tried to address in my proposal about factory initializers (based on all previous discussions on that topic).
I’d love to see this happen, but much like the reason why I didn’t push the factory initializers proposal to review, I think now’s not the time for this.
The swift dev team has made it clear that they have very strict priorities and stages regarding the release schedule.
I’m afraid at this time, anything that is not a bug-fix or doesn’t align with the current stage’s priorities will be promptly dismissed.
The sad part is that currently there’s no good way of stashing proposals for future revision, so we just gotta keep these in our heads until the time is right.

> On Jul 25, 2017, at 12:44 PM, philohan95 via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I think the current way to initiate models in a Failable Initializer `init?()` is overly verbose and should be shortened down so less boilerplate should be needed.
> 
> The current way:
> 
> ```
> let someProperty: Any
> let anotherProperty: Any
> 
> init?(data: [String: Any]) {
> 	guard
> 		let someProperty = data["some_key"],
> 		let anotherProperty = data["another_key"]
> 	else {
> 		return nil
> 	}
> 
> 	self. someProperty = someProperty
> 	self. anotherProperty = anotherProperty
> }
> ```
> 
> As you can see we had to use the properties twice (this would also be the case of `if let`) making the initializer twice as long as necessary and becomes a pain to implement when having more than 1 property.
> 
> My idea is extending the power of the `guard` statement
> 
> Idea:
> 	init?(data: [String: Any]) {
> 		guard
> 			someProperty = data["some_key"], // Currently fails because `self` us used before all stored properties are initialized
> 			anotherProperty = data["another_key"]
> 		else {
> 			return nil
> 		}
> 	}
> }
> 
> _______________________________________________
> 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