[swift-evolution] [Proposal Draft] Flexible memberwise initialization

Howard Lovatt howard.lovatt at gmail.com
Mon Jan 4 17:48:57 CST 2016


Yes you can get close, but:

1. Its weird that you can only do this in an extension.
2. Its not quite the same as the proposal the current member-wise initialiser does not make the init arguments optional (the arguments themselves do not have defaults), i.e. with your example `let defaultOriginRect = Rect(size: Size(width: 5.0, height: 5.0))` fails whereas it would work for the proposal (this could also be true if the existing struct memberwise init and the new `memberwise init(..)` where changed to provide init argument defaults).
3. Only ‘really' works for structs, the compiler doesn’t write a member-wise initialiser for classes (just a default initialiser).
4. Still need the compiler to provide both default and member-wise initialisers, whereas this proposal would allow the existing default and member-wise initialisers to be deprecated and just the new member-wise initialiser would remain which would simplify the language and make it clear what was happening (this could also be true if a `memberwise init(..)` where added and existing compiler written inits removed).


> On 5 Jan 2016, at 10:16 AM, Matthew Johnson <matthew at anandabits.com> wrote:
> 
> struct Rect { var origin: Point = Point(), size: Size = Size() }
> extension Rect {
>        init(center: Point, size: Size) {
>            let originX = center.x - (size.width / 2)
>            let originY = center.y - (size.height / 2)
>            self.init(origin: Point(x: originX, y: originY), size: size)
>        }
> }

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


More information about the swift-evolution mailing list