Is there a way to pass #file
and #line
into an instance of a type conforming to ExpressibleByStringInterpolation
when it is only initialized by a string? For example, an instance of the type
struct Example {
let code: String
let file: StaticString
let line: UInt
init(code: String, file: StaticString = #file, line: UInt = #line) {
self.code = code
self.file = file
self.line = line
}
}
can be initialized by let example = Example("let a = 1")
. It knows from which file and line it comes as file
and line
will be populated by the default arguments.
Now, it would be quite handy to have Example
conform to ExpressibleByStringInterpolation
so that I can just write let example: Example = "let a = 1"
to initialize an example:
extension Example: ExpressibleByStringInterpolation {
public init(stringLiteral code: String) {
self.init(code: code)
}
}
However, logically #file
and #line
will refer to the init(stringLiteral:)
initializer, not the place where example
is created which I would like to have instead. Is there a way to achieve this?