Position of operator usage via #file and #line

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

There's a bug open for this: https://bugs.swift.org/browse/SR-11885 . 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.