In Swift 3, I wrote a custom operator, the prefix operator § , which I use in a method that takes a String value as it returns a LocalizedString structure (holding down the key and value).
public prefix func §(key: String) -> LocalizedString { return LocalizedString(key: key) } public struct LocalizedString { public var key: String public var value: String public init(key: String) { let translated = translate(using: key)
(Yes, I know about the awesome L10n enum in SwiftGen , but we are loading our lines from our backend, and this question is more about how to work with custom operators)
But what if we want to get the translated value from the result of the § operator (i.e., the value property from the resulting LocalizedString )
let translation = §"MyKey".value
We can, of course, easily fix this compilation error by crushing it in brackets (§"MyKey").value . But if you do not want to do this. Is it possible to set priority for user statements in relation to a literal ?
Yes, I know that only infix operators can declare priority, but it would be advisable to somehow work with priority in order to achieve what I want:
precedencegroup Localization { higherThan: DotPrecedence } // There is no such group as "Dot" prefix operator §: Localization
To note that the Swift compiler must first evaluate §"MyKey" and understand that this is not a string, but actually a LocalizedString (struct).
Feels that this is impossible? What am I missing?