Initialiser Helper


(James Campbell) #1

I would like to be able to use functions to help me initilise a class or
struct so I can break down a init into seperate methods, like so:

struct Person {

let age: Int

init (json: Dictionary) {
// lots of code

age = json["age"]
}

}

This can become un-wieldly, so I would love to be able to do this:

struct Person {

let age: Int

init (json: Dictionary) {
// lots of code

age = getAge(json)
}

private func getAge(json: Dictionary) -> Int {

}

}

···

*___________________________________*

*James⎥*

*james@supmenow.com <james@supmenow.com>⎥supmenow.com <http://supmenow.com>*

*Sup*

*Runway East *

*10 Finsbury Square*

*London*

* EC2A 1AF *


(Matthew Johnson) #2

You might be interested in taking a look at my proposal for partial initializers https://github.com/anandabits/swift-evolution/blob/partial-initializers/proposals/NNNN-partial-initializers.md

This proposal was tabled for Swift 3 but I hope to revisit the topic of improving initializers in when we ramp up Swift 4.

···

Sent from my iPad

On May 23, 2016, at 8:24 AM, James Campbell via swift-evolution <swift-evolution@swift.org> wrote:

I would like to be able to use functions to help me initilise a class or struct so I can break down a init into seperate methods, like so:

struct Person {

let age: Int

init (json: Dictionary) {
// lots of code

age = json["age"]
}

}

This can become un-wieldly, so I would love to be able to do this:

struct Person {

let age: Int

init (json: Dictionary) {
// lots of code

age = getAge(json)
}

private func getAge(json: Dictionary) -> Int {

}

}
___________________________________

James⎥

james@supmenow.com⎥supmenow.com

Sup

Runway East >

10 Finsbury Square

London

> EC2A 1AF

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(James Campbell) #3

Looks like it :slight_smile:

···

*___________________________________*

*James⎥Head of Trolls*

*james@supmenow.com <james@supmenow.com>⎥supmenow.com <http://supmenow.com>*

*Sup*

*Runway East *

*10 Finsbury Square*

*London*

* EC2A 1AF *

On 23 May 2016 at 14:34, Matthew Johnson <matthew@anandabits.com> wrote:

You might be interested in taking a look at my proposal for partial
initializers
https://github.com/anandabits/swift-evolution/blob/partial-initializers/proposals/NNNN-partial-initializers.md

This proposal was tabled for Swift 3 but I hope to revisit the topic of
improving initializers in when we ramp up Swift 4.

Sent from my iPad

On May 23, 2016, at 8:24 AM, James Campbell via swift-evolution < > swift-evolution@swift.org> wrote:

I would like to be able to use functions to help me initilise a class or
struct so I can break down a init into seperate methods, like so:

struct Person {

let age: Int

init (json: Dictionary) {
// lots of code

age = json["age"]
}

}

This can become un-wieldly, so I would love to be able to do this:

struct Person {

let age: Int

init (json: Dictionary) {
// lots of code

age = getAge(json)
}

private func getAge(json: Dictionary) -> Int {

}

}

*___________________________________*

*James⎥*

*james@supmenow.com <james@supmenow.com>⎥supmenow.com
<http://supmenow.com>*

*Sup*

*Runway East *

*10 Finsbury Square*

*London*

* EC2A 1AF *

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(L Mihalkovic) #4

-1 would not miss it if it never made it to the language.

Regards
(From mobile)

···

On May 23, 2016, at 3:34 PM, Matthew Johnson via swift-evolution <swift-evolution@swift.org> wrote:

You might be interested in taking a look at my proposal for partial initializers https://github.com/anandabits/swift-evolution/blob/partial-initializers/proposals/NNNN-partial-initializers.md

This proposal was tabled for Swift 3 but I hope to revisit the topic of improving initializers in when we ramp up Swift 4.

Sent from my iPad

On May 23, 2016, at 8:24 AM, James Campbell via swift-evolution <swift-evolution@swift.org> wrote:

I would like to be able to use functions to help me initilise a class or struct so I can break down a init into seperate methods, like so:

struct Person {

let age: Int

init (json: Dictionary) {
// lots of code

age = json["age"]
}

}

This can become un-wieldly, so I would love to be able to do this:

struct Person {

let age: Int

init (json: Dictionary) {
// lots of code

age = getAge(json)
}

private func getAge(json: Dictionary) -> Int {

}

}
___________________________________

James⎥

james@supmenow.com⎥supmenow.com

Sup

Runway East >>

10 Finsbury Square

London

>> EC2A 1AF

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Patrick Smith) #5

You could move getAge() to be a private func outside of the scope of Person?

···

On 23 May 2016, at 11:24 PM, James Campbell via swift-evolution <swift-evolution@swift.org> wrote:

I would like to be able to use functions to help me initilise a class or struct so I can break down a init into seperate methods, like so:

struct Person {

let age: Int

init (json: Dictionary) {
// lots of code

age = json["age"]
}

}

This can become un-wieldly, so I would love to be able to do this:

struct Person {

let age: Int

init (json: Dictionary) {
// lots of code

age = getAge(json)
}

private func getAge(json: Dictionary) -> Int {

}

}
___________________________________

James⎥

james@supmenow.com <mailto:james@supmenow.com>⎥supmenow.com <http://supmenow.com/>
Sup

Runway East >

10 Finsbury Square

London

> EC2A 1AF

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution