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

proposal: spec: generic should allow convert string type back to generic type when the generic type is string. #62658

Closed
bronze1man opened this issue Sep 15, 2023 · 4 comments

Comments

@bronze1man
Copy link
Contributor

bronze1man commented Sep 15, 2023

I have tried four ways to convert string type back to generic type when the generic type is string. The last one with *string success.
I think it is too difficult for a new user to finish this task.

It may be good to just allow my first try success when the dst_t is string and compile error when the dst_t is not string.

func To1[dst_t any](src any) (dst dst_t) {
	switch (any)(dst).(type) {
	case string:
		switch obj := src.(type) {
		case int:
			dst = dst_t(strconv.FormatInt(int64(obj), 10))
			return dst
		}
	}
	return dst
}

Here is my code: https://go.dev/play/p/Bst6mDAjN7g

@gopherbot gopherbot added this to the Proposal milestone Sep 15, 2023
@seankhliao
Copy link
Member

Duplicate of #45380

@seankhliao seankhliao marked this as a duplicate of #45380 Sep 15, 2023
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Sep 15, 2023
@bronze1man
Copy link
Contributor Author

Duplicate of #45380

No, I do not want "type switching on type parameters" in this proposal.
I want allow convert string type back to generic type when the generic type is string.
It is not same thing.

@atdiar
Copy link

atdiar commented Sep 17, 2023

I think it is linked to type parameter switches but that's an interesting issue.

The current implementation considers that the type parameter is an interface with the dynamic type being fixed at instantiation.

So dst_t is not string but its dynamic type is which is what you are testing here. That's why I think the conversion does not work, nor does returning a string.

Maybe it should be treated as an existential type rather, in spite of how it is implemented.

@ianlancetaylor
Copy link
Contributor

This is a dup of type switches on type parameters because you are looking for a way to treat a type parameter specially based on a type switch on that type parameter. You want to be able to say that in case string the compiler should know that the type is string. That is part of #45380.

Today the compiler does not know assume anything based on a type switch.

@golang golang locked and limited conversation to collaborators Sep 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants