I was working on my custom unwrap operator and the limitations of the current syntax (making it a function doesn't allow modification, making it a property doesn't allow to put extra default parameters like file/line, ditto for making it an operator (there's an outstanding bug about default parameters support in operators), making it a subscript makes it look really weird) got me to the following idea: allow default parameters for getters, setters & friends:
var value: Int {
get(line: Int = #line) {
print("value getter called at \(line)")
return 42
}
set(line: Int = #line) {
print("value setter called at \(line), value: \(newValue)")
}
}
ditto for didSet
/ willSet
, & friends
As it is now, the main parameter name in setter (newValue / oldValue) could be customised, in which case it will be the first item in the parameter list and it will not have the type declaration.
var value: Int {
get(line: Int = #line) {
...
}
set(val, line: Int = #line) {
print("value setter called at \(line), value: \(val)")
}
}
extra parameters would have to be defaulted:
var value: Int {
get {...}
set(val, line) {...} // ๐ missing type declaration
set(val, line: Int) {...} // ๐ missing default value
set(line: Int) {...} // ๐ missing default value
}
The use site of getters / setters won't change and there will be no way to pass the parameters explicitly (this will be in line with the upcoming support of default parameters for the operators):
value(line: 1) = 1 // ๐
_ = value(line: 1) // ๐
2 * (line: 1) 3 // ๐
2 * (line: 1) 3 // ๐
(*)(2, 3, line: 1) // โ