Yes, using the same example, you can make required init to be convenience init:
class MyBase {
required init (coder: Int) {
}
required init (frame: Int) {
}
}
class MyClass : MyBase {
let x:Int
let y:String
let z:Double
private init(isCoder: Bool, _ xValue:
There's nothing stopping you from making init(frame:) and init?(coder:)
convenience:
public class ColorView: NSView {
public required convenience init?(coder: NSCoder) {
self.init(frame: CGRect(x: 0.0, y: 0.0, width: 20.0, height:
20.0))
}
Good question/idea.
The use case I have with this most often is -initWithCoder and some other init
like -initWithFrame. I don’t think you can make those convenience inits. If
there is a way to make that work though, I am all for it.
The thing I do most often, when possible, is lazily set
The question is if we can solve the problem with special private init() and
convenience inits ?
class MyBase {
init () {
}
}
class MyClass : MyBase {
let x:Int
let y:String
let z:Double
private init(_ xValue: Int, _ zValue: Double) {
self.x = xValue
I don’t remember a proposal for that, but I would definitely support one. I
use this pattern all the time (usually by initializing to a default value
first), and it would be nice to have an explicit/safe way to handle it, (and to
avoid waisting cycles creating an object I never use).
Perhaps
I believe there was(was?) already a suggestion to introduce special methods
that could be called from initializers. IMO this is a better solution to
the problem as you really should not call 'usual' instance method until the
instance is fully instantiated(super.init() called in your case):
Maybe there are several initialisers, with different ways of calculating 'y'
depending on what you give it.
Karl
>
> On Jun 15, 2016 at 2:26 PM, (mailto:char...@charliemonroe.net)> wrote:
>
>
>
> Is there a particular reason for not using lazy var here?
Say you have the following code. There is a property on MyClass (‘y’) which is
derived from other data via an instance method; Two-stage initialisation.
class MySuperClass {
init() {}
}
class MyClass : MySuperClass {
let x : Int
var y : String
init(x: Int) {