Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unrestricting explicit declarations of function generics. #77483

Open
fenginsc opened this issue Nov 8, 2024 · 2 comments
Open

Unrestricting explicit declarations of function generics. #77483

fenginsc opened this issue Nov 8, 2024 · 2 comments
Labels
feature A feature request or implementation triage needed This issue needs more specific labels

Comments

@fenginsc
Copy link

fenginsc commented Nov 8, 2024

Motivation

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

@fenginsc fenginsc added feature A feature request or implementation triage needed This issue needs more specific labels labels Nov 8, 2024
@xwu
Copy link
Collaborator

xwu commented Nov 9, 2024

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:

https://forums.swift.org/t/proposal-allow-explicit-type-parameter-specification-in-generic-function-call/4583/7

@fenginsc
Copy link
Author

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:

https://forums.swift.org/t/proposal-allow-explicit-type-parameter-specification-in-generic-function-call/4583/7

I may need to be there to participate in the discussion.
thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A feature request or implementation triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

2 participants