If we're not going the #context way then I would also like to pitch the need for #module static string. And as mentioned in the other thread I would like to have a general #type(T) to extract generic type parameter as a static string (if that's technically possible). Or something like this maybe:
extension StaticString {
init<T>(_ type: T.Type) { ... }
}
// The rest would be trivial
func work<T>(_ type: StaticString = .init(T.self)) { ... }
2 Likes
LowAmmo
(Kris Kline)
22
I realize this topic is now 4 years old. But just curious on any plans to possibly add an ability to include "#self" or "#Self" to Swift.
An earlier comment mentioned XCTestExpecation as a good use case, which is exactly the use case that would be helpful for me.
Trying to create a helpful function for testing, and would like to pass in a default "#self" parameter to the function, to be able to create expectations on that specific Test Case, so that waiting for expectations is done on that test.
Obviously, I can do this in other ways by just adding an optional parameter, or by just operating on a new instance of the test. (I could also not be so stubborn and make the function into a method of the testing class so I would easily have access to 'self').
Just trying to "have my cake and eat it too", and curious if anymore thought has gone into possibly have "#self" be a supported command for a default parameter that would grab the "self" of the calling object. So, be able to define something like:
public func XCTWaitForLogging(test: XCTestCase = #self) {
let expectation = test.expectation(description: "wait for logging queue")
staticLogger.waitForQueue {
expectation.fulfill()
}
test.wait(for: [expectation], timeout: 10)
}
...
XCTWaitForLogging()
...
// Instead of
XCTWaitForLogging(self)
As other posters also called out, there could potentially be some other cooler things that could be done. Just would like to have that ability...
tera
23
Never managed to have this working apart from passing individual # params:
func foo(funcAndLine v: (String, Int) = (#function, #line), function: String = #function, line: Int = #line) {
print(v.0, v.1) // 😢 foo(funcAndLine:function:line:) 17
print(function, line) // 😃 bar() 23
}
func bar() {
foo()
}
bar()
This doesn't give the wanted result either:
func foo(funcAndLine: String = #function + String(#line), ......)
or even this:
func foo(funcAndLine: String = #function + "!", .....)
You may like #fileID instead which makes "ModuleName/fileName.swift" formatted string.
LowAmmo
(Kris Kline)
24
Another interesting application - you could define a method or a function that is only callable from certain classes, by enforcing that "self" (of the calling class/method) conforms to a certain protocol. Would be kind of cool to be sort of a more dynamic "delegate" model - where the object being called into doesn't have to store the delegate...
Just trying to think of any other justification I can for why adding in "#self" would be a worthwhile enhancement... 
LowAmmo
(Kris Kline)
26
Could also just require that use of self is always optional, so consumers can override and pass in "nil" if they want to limit what a function has access to.
1 Like