This is what I want: member property to be a let, with default value that can be overridden and make use of compiler auto generated init().
// This is how I want write out: `x` is `let`, with default overridable value
struct Foo {
let x: Int? = nil
}
let foo = Foo(x: 123) // **error:** **argument passed to call that takes no arguments**
So I must write out the init:
Struct Foo {
let x: Int?
init(x: Int? = nil) {
self.x = x
}
}
Or change x to a var:
struct Foo {
var x: Int? = nil
}
// So now this work, but x is a `var` :(
let foo = Foo(x: 123)
Why do you want to do what you've stated? As I read the language spec, it kind of goes against what a let (a constant value that can't be changed once initialized) is. If you want a constant value that gets initialized at run-time with a value, then you have to specify the initializer (your second case). In the first case, you initialized x with a nil value, which sets x's value for the remainder of the execution, if I read the language reference correctly. In your first example, you initialized x in the declaration, thus, the auto-generated init has nothing to do, and, since you only have one instance variable, it's a constant, and it's already initialized, the initializer doesn't have any arguments.
private(set) still allows you to mutate the value, just greatly constrains where it can get set, namely, within the object that owns the property. If you want a constant that can be set at runtime, and can't be mutated at all, this is a pretty good approach. But, you have to write the initializer.
struct Foo: View {
@Binding var value1: Double
@Binding var value2: Double
// etc, etc
let onTouch: (() -> ())? // these should have default nil value
let onLongPress: (() -> ())?
let iconFactory: ((Double) -> Image)?
// etc, etc
}
I want the optional callback closures to be let and default nil, at the same time, I want to avoid having to write the initializer...it's kind of tedious and anytime I add or subtract something, I must revise the hand written initializer. Seem like a perfect task for the compiler
(should compiler generated init() support let property with default value?)