Proposal: Automatic initializer generation

A lot of initializers tediously assign values to variables which results in a lot of code such as self.variable = arg1 (or even worse variable = variable), mostly for classes that are meant to just encapsulate several values.

I propose adding auto keyword (to be discussed - anyone has a better name in mind?), which would automatically assign same-named variables. Example:

class User {
  var name: String
  var password: String
  
  init(auto name: String, auto password: String) {
    // No assignment required, the variables will be automatically assigned.
    // Perform additional init stuff here.
  }
}

This would, of course, work only if the argument has the same name as a stored variable on the class.

Additionally, if the class is root, or the superclass has an initializer that takes no arguments, I propose adding @auto_init annotation, which would generate a default initializer, similar to what is done for structs:

@auto_init
class User {
  var name: String
  var password: String
}

Normally, such class would be illegal since it would have no accessible initializers. The annotation could specify the access control as well: @auto_init(private), @auto_init(internal), @auto_init(public).

If the class isn't root, but inherits from an object that has an initializer that takes no arguments (e.g. NSObject), this would be allowed as well and the initializer with no arguments would be called on super.

Any thoughts on this? Sorry, if this has been already discussed.

Charlie

Discussed last month [swift-evolution] [Pitch] Unifying init parameters with properties
And (linked from that thread) last year http://article.gmane.org/gmane.comp.lang.swift.evolution/727

I think it’s a good idea, but discussion seems to have just petered out without a formal proposal both times.

How about we just apply the struct auto-init behavior to classes? It’s nice and simple, and already in the language.

···

On May 23, 2016, at 7:29 AM, Charlie Monroe via swift-evolution <swift-evolution@swift.org> wrote:

A lot of initializers tediously assign values to variables which results in a lot of code such as self.variable = arg1 (or even worse variable = variable), mostly for classes that are meant to just encapsulate several values.

I propose adding auto keyword (to be discussed - anyone has a better name in mind?), which would automatically assign same-named variables. Example:

class User {
  var name: String
  var password: String
  
  init(auto name: String, auto password: String) {
    // No assignment required, the variables will be automatically assigned.
    // Perform additional init stuff here.
  }
}

This would, of course, work only if the argument has the same name as a stored variable on the class.

Additionally, if the class is root, or the superclass has an initializer that takes no arguments, I propose adding @auto_init annotation, which would generate a default initializer, similar to what is done for structs:

@auto_init
class User {
  var name: String
  var password: String
}

Normally, such class would be illegal since it would have no accessible initializers. The annotation could specify the access control as well: @auto_init(private), @auto_init(internal), @auto_init(public).

If the class isn't root, but inherits from an object that has an initializer that takes no arguments (e.g. NSObject), this would be allowed as well and the initializer with no arguments would be called on super.

Any thoughts on this? Sorry, if this has been already discussed.

Charlie

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

Discussed last month [swift-evolution] [Pitch] Unifying init parameters with properties
And (linked from that thread) last year http://article.gmane.org/gmane.comp.lang.swift.evolution/727

I think it’s a good idea, but discussion seems to have just petered out without a formal proposal both times.

This didn’t peter out. My opinion on this topic evolved throughout the review. The perspective I came to is that the best approach may require changes that are outside the scope of Swift 3. I expect this to come up again during the Swift 4 conversation.

···

On May 23, 2016, at 10:52 AM, Kevin Nattinger via swift-evolution <swift-evolution@swift.org> wrote:

How about we just apply the struct auto-init behavior to classes? It’s nice and simple, and already in the language.

On May 23, 2016, at 7:29 AM, Charlie Monroe via swift-evolution <swift-evolution@swift.org> wrote:

A lot of initializers tediously assign values to variables which results in a lot of code such as self.variable = arg1 (or even worse variable = variable), mostly for classes that are meant to just encapsulate several values.

I propose adding auto keyword (to be discussed - anyone has a better name in mind?), which would automatically assign same-named variables. Example:

class User {
  var name: String
  var password: String
  
  init(auto name: String, auto password: String) {
    // No assignment required, the variables will be automatically assigned.
    // Perform additional init stuff here.
  }
}

This would, of course, work only if the argument has the same name as a stored variable on the class.

Additionally, if the class is root, or the superclass has an initializer that takes no arguments, I propose adding @auto_init annotation, which would generate a default initializer, similar to what is done for structs:

@auto_init
class User {
  var name: String
  var password: String
}

Normally, such class would be illegal since it would have no accessible initializers. The annotation could specify the access control as well: @auto_init(private), @auto_init(internal), @auto_init(public).

If the class isn't root, but inherits from an object that has an initializer that takes no arguments (e.g. NSObject), this would be allowed as well and the initializer with no arguments would be called on super.

Any thoughts on this? Sorry, if this has been already discussed.

Charlie

_______________________________________________
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

Thanks for the links.

You can't apply auto-init behavior to all classes since it gets a bit more complicated once the only initializers available take some arguments - the compiler can't know what to pass where to super, so it would need to chain the arguments, which would lead to initializers with dozens of arguments. So in such case the auto-init is IMHO out of question.

The variant that I'm suggesting is simplified and restricted only to classes that either do not inherit from anything, or have an initializer that takes no arguments - which makes the issue much simpler than previously suggested.

I dislike the syntax of the previous proposal

init (self.a: Int, self.b: String) {
   //...
}

where you specify self.a: Int - IMHO this should be done using a keyword that automates it and I'd allow it only for arguments that match a variable on the instance. Or perhaps allowing something like auto(xyz) to specify the variable? Or #auto(xyz)?

This seems a bit more readable to me:

init (auto a: Int, auto b: String) {
   //...
}

If there is still some interest in making this a proposal, I can write it up.

Charlie

···

On May 23, 2016, at 5:52 PM, Kevin Nattinger <swift@nattinger.net> wrote:

Discussed last month [swift-evolution] [Pitch] Unifying init parameters with properties
And (linked from that thread) last year http://article.gmane.org/gmane.comp.lang.swift.evolution/727

I think it’s a good idea, but discussion seems to have just petered out without a formal proposal both times.

How about we just apply the struct auto-init behavior to classes? It’s nice and simple, and already in the language.

On May 23, 2016, at 7:29 AM, Charlie Monroe via swift-evolution <swift-evolution@swift.org> wrote:

A lot of initializers tediously assign values to variables which results in a lot of code such as self.variable = arg1 (or even worse variable = variable), mostly for classes that are meant to just encapsulate several values.

I propose adding auto keyword (to be discussed - anyone has a better name in mind?), which would automatically assign same-named variables. Example:

class User {
  var name: String
  var password: String
  
  init(auto name: String, auto password: String) {
    // No assignment required, the variables will be automatically assigned.
    // Perform additional init stuff here.
  }
}

This would, of course, work only if the argument has the same name as a stored variable on the class.

Additionally, if the class is root, or the superclass has an initializer that takes no arguments, I propose adding @auto_init annotation, which would generate a default initializer, similar to what is done for structs:

@auto_init
class User {
  var name: String
  var password: String
}

Normally, such class would be illegal since it would have no accessible initializers. The annotation could specify the access control as well: @auto_init(private), @auto_init(internal), @auto_init(public).

If the class isn't root, but inherits from an object that has an initializer that takes no arguments (e.g. NSObject), this would be allowed as well and the initializer with no arguments would be called on super.

Any thoughts on this? Sorry, if this has been already discussed.

Charlie

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

From what I have read of the proposals, they were IMO premature considering some questions not yet tabled (on the 4.0 train if i recall). Might be part of the reason for the core teams not pressing this agenda forward today.

···

On May 23, 2016, at 5:52 PM, Kevin Nattinger via swift-evolution <swift-evolution@swift.org> wrote:

Discussed last month [swift-evolution] [Pitch] Unifying init parameters with properties
And (linked from that thread) last year http://article.gmane.org/gmane.comp.lang.swift.evolution/727

I think it’s a good idea, but discussion seems to have just petered out without a formal proposal both times.

How about we just apply the struct auto-init behavior to classes? It’s nice and simple, and already in the language.

On May 23, 2016, at 7:29 AM, Charlie Monroe via swift-evolution <swift-evolution@swift.org> wrote:

A lot of initializers tediously assign values to variables which results in a lot of code such as self.variable = arg1 (or even worse variable = variable), mostly for classes that are meant to just encapsulate several values.

I propose adding auto keyword (to be discussed - anyone has a better name in mind?), which would automatically assign same-named variables. Example:

class User {
   var name: String
   var password: String
   
   init(auto name: String, auto password: String) {
       // No assignment required, the variables will be automatically assigned.
       // Perform additional init stuff here.
   }
}

This would, of course, work only if the argument has the same name as a stored variable on the class.

Additionally, if the class is root, or the superclass has an initializer that takes no arguments, I propose adding @auto_init annotation, which would generate a default initializer, similar to what is done for structs:

@auto_init
class User {
   var name: String
   var password: String
}

Normally, such class would be illegal since it would have no accessible initializers. The annotation could specify the access control as well: @auto_init(private), @auto_init(internal), @auto_init(public).

If the class isn't root, but inherits from an object that has an initializer that takes no arguments (e.g. NSObject), this would be allowed as well and the initializer with no arguments would be called on super.

Any thoughts on this? Sorry, if this has been already discussed.

Charlie

_______________________________________________
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

Discussed last month [swift-evolution] [Pitch] Unifying init parameters with properties
And (linked from that thread) last year http://article.gmane.org/gmane.comp.lang.swift.evolution/727

I think it’s a good idea, but discussion seems to have just petered out without a formal proposal both times.

IMHO the proposals were on a collision course with yet untouched parts of the language.

···

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

On May 23, 2016, at 10:52 AM, Kevin Nattinger via swift-evolution <swift-evolution@swift.org> wrote:

This didn’t peter out. My opinion on this topic evolved throughout the review. The perspective I came to is that the best approach may require changes that are outside the scope of Swift 3. I expect this to come up again during the Swift 4 conversation.

How about we just apply the struct auto-init behavior to classes? It’s nice and simple, and already in the language.

On May 23, 2016, at 7:29 AM, Charlie Monroe via swift-evolution <swift-evolution@swift.org> wrote:

A lot of initializers tediously assign values to variables which results in a lot of code such as self.variable = arg1 (or even worse variable = variable), mostly for classes that are meant to just encapsulate several values.

I propose adding auto keyword (to be discussed - anyone has a better name in mind?), which would automatically assign same-named variables. Example:

class User {
   var name: String
   var password: String
   
   init(auto name: String, auto password: String) {
       // No assignment required, the variables will be automatically assigned.
       // Perform additional init stuff here.
   }
}

This would, of course, work only if the argument has the same name as a stored variable on the class.

Additionally, if the class is root, or the superclass has an initializer that takes no arguments, I propose adding @auto_init annotation, which would generate a default initializer, similar to what is done for structs:

@auto_init
class User {
   var name: String
   var password: String
}

Normally, such class would be illegal since it would have no accessible initializers. The annotation could specify the access control as well: @auto_init(private), @auto_init(internal), @auto_init(public).

If the class isn't root, but inherits from an object that has an initializer that takes no arguments (e.g. NSObject), this would be allowed as well and the initializer with no arguments would be called on super.

Any thoughts on this? Sorry, if this has been already discussed.

Charlie

_______________________________________________
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

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