struct ReversedStyle<Subject>: FormatStyle {
func format(_ value: Subject) -> String {
String(String(describing: value).reversed())
}
}
// this works but rather not use func
extension FormatStyle where Self == ReversedStyle<Any> {
static func reversed<Subject>() -> ReversedStyle<Subject> { .init() }
}
// this is preferred, this compile but no easy way to call
extension FormatStyle where Self == ReversedStyle<Any> {
// Is there anyway to change the above to a var:
static var reversedVar: Self { .init() }
}
// this works!
25.formatted(.reversed())
// but I prefer to use as a var syntax, how to make it work?
25.formatted(.reversedVar) // error: Instance method 'formatted' requires the types 'Int' and 'Any' be equivalent
Unfortunately computed properties can't define generics, so — to my knowledge — it isn't possible.