Some users are likely to know that Swift allows you to override the default inferred type of literals by declaring specific typealiases in their code:
typealias StringLiteralType = CustomStringType
let a = "hello"
print(type(of: a)) // CustomStringType
However, this overriding is only valid at the top level. Trying to do something like below, where the user wants to scope the inference of the default literal types, isn't supported:
class MyClass {
// Inside of class I want string literals to be typed as CustomStringType
private typealias StringLiteralType = CustomStringType
func testing() -> String {
let t = "hello" // I want a CustomStringType
return String(fromCustom: t) // t was inferred to be `String`, not our custom type
}
}
I can see some reasons behind only allowing overriding of the inferred literal types at the top level, mostly
around reducing complexity in the type checker. But are there are concrete reasons that this behavior shouldn't be allowed? I'm curious to know if people would be open to expanding this "feature" to allow more fine grain control.
And at a more fundamental level, was this "feature" ever really meant to exist in a user facing way, or was it more of a outcome of how type inference was implemented?