You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, functions cannot explicitly declare generic parameters, for example:
func foo<T>() -> T.Type {
return T.self
}
var type = foo<Int>()
| `- note: while parsing this '<' as a type parameter bracket
`- error: cannot explicitly specialize a generic function
if you want to realize the function foo need to be implemented in a different way:
class MyClass<T>{
var type = T.self
}
struct MyStruct<T> {
var type = T.self
}
var x = MyClass<Int>().type
print(x)
var y = MyStruct<String>().type
print(y)
So why can't we just support specifying specific generalizations?
Then there is the case when the type derivation is not the type the user wants:
func foo2<T>(value: T) -> T.Type {
return T.self
}
var z = foo2(value: 3)
print(z) //z is Int.Type
But I have to write it like this here if I want 3 to be of type Int64:
var z = foo2(value: 3 as Int64)
print(z) //z is Int64.Type
Proposed solution
So it might be better to explicitly declare the type of the generalization, or to make the language easier to use, as in the example above:
var type = foo<Int>()
var z = foo2<Int64>(value: 3)
print(z) //z is Int64.Type
thanks.
Alternatives considered
No response
Additional information
No response
The text was updated successfully, but these errors were encountered:
Hi @fenginsc, language features are discussed on the Swift forums. You'll find that this particular request has been discussed before a few times. Fo example:
Hi @fenginsc, language features are discussed on the Swift forums. You'll find that this particular request has been discussed before a few times. Fo example:
Motivation
Currently, functions cannot explicitly declare generic parameters, for example:
if you want to realize the function
foo
need to be implemented in a different way:So why can't we just support specifying specific generalizations?
Then there is the case when the type derivation is not the type the user wants:
But I have to write it like this here if I want
3
to be of typeInt64
:Proposed solution
So it might be better to explicitly declare the type of the generalization, or to make the language easier to use, as in the example above:
thanks.
Alternatives considered
No response
Additional information
No response
The text was updated successfully, but these errors were encountered: