Could you please provide a link to the issue/PR/pitch for this upcoming feature?
This feature would also be nice to have for methods that satisfy a protocol requirement. For example, a conformance to ExpressibleByStringLiteral requires to implement the initializer init(stringLiteral:). I wish there was a way to pass further information on to the initializer while at the same time the requirements of ExpressibleByStringLiteral are fulfilled:
struct StringWrapper: ExpressibleByStringLiteral {
let value: String
let line: Int
init(stringLiteral value: String, line: Int = #line) {
self.value = value
self.line = line
}
}
However, while I look forward to have a way to pass information from the call side on to an initializer/a method/a computed property/..., I dislike the idea of misusing default parameters for that.
In the case of operators, there is no way to ever pass a real argument instead of the default. There would be an operator declaration with many arguments which just serve no purpose for the user. Even worse for getters. Why would getters have arguments at all? They are supposed to provide values, not the other way around.
In all the requests for this or similar features, I have only seen #line and #file as examples and both would be my only use case as well. Are there other examples of default arguments or is it really always about these two?
If not, I propose the new macros #callerFile and #callerLine which would be accessible in any kind of callable object. Instead of
func location(path: String = #file, line: Int = #line) -> String {
path + ":\(line)"
}
we could just write:
func location() -> String {
#callerPath + ":\(#callerLine)"
}
This would work in initializers, operators, methods, getters, setters, subscripts — you name it — without specifying (potentially confusing) additional parameters.