In Swift you can find out from where your function was called by providing the default arguments #file and #line, like so:
func myFunc(x : T, y : T, file : String = #file, line : Int = #line) -> T {
print("Being called at \(file):\(line)")
return someComputation(x, y)
}
Is it somehow possible to achieve the same for (infix) operators? Direct translation of the above doesn't work:
func == (x : T, y : T, file : String = #file, line : Int = #line) -> T {
print("Being called at \(file):\(line)")
return someComputation(x, y)
}
will predictably give an error along the lines of "operators must have one or two arguments".
My concrete problem is that I have a DSL which uses infix operators, and I would like to know where those infix operators were called for better error messages.
1 Like
owenv
(Owen Voorhees)
2
There's a bug open for this: [SR-11885] Allow operator functions to have extra arguments with default values · Issue #54301 · apple/swift · GitHub . It would be fairly easy to support if someone came up with a well-motivated evolution proposal. As far as I know there's no way to achieve what you want today unfortunately.
1 Like
Ok, thank you very much for that pointer. My use case might mostly disappear once function builders support buildExpr, as I could then attach #file and #line default parameters to it instead.
tera
4
I came across the recent pitch draft and implementation, is that not ready yet? cc @mininny