We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
go version
$ go version go1.19.1 windows/amd64
yes
type Command struct { Path string Env []string Cmd string Args []string } type Rename struct { Path string Regexp string Target string } //... type Ruler interface { Command | Replace | Remove | Rename | Copy | Make } type Action struct { Type Type //Enumeration values bound to Ruler types Commands []Command Replaces []Replace Removes []Remove Renames []Rename Copies []Copy Makes []Make } func getRulers[R Ruler](action Action) []R { switch action.Type { case TypeCommand: return action.Commands case TypeReplace: return action.Replaces case TypeRemove: return action.Removes case TypeRename: return action.Renames case TypeCopy: return action.Copies case TypeMake: return action.Makes } return nil }
If Ruler is the Command type, then the []R I return is []Command But the compiler does not recognize this
Ruler
Command
[]R
[]Command
.\action.go:48:10: cannot use action.Commands (variable of type []Command) as type []R in return statement
Now I can only force the type conversion to R:
func castRuleCommand[R Ruler](r []Command) []R { return *(*[]R)(unsafe.Pointer(&r)) }
The code can be compiled
The text was updated successfully, but these errors were encountered:
This is working as intended, in your example if you R is instantiated to Command, then all your other return statements are invalid.
R
See also #45380
Sorry, something went wrong.
No branches or pull requests
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What did you do?
What did you expect to see?
If
Ruler
is theCommand
type, then the[]R
I return is[]Command
But the compiler does not recognize this
Now I can only force the type conversion to R:
What did you see instead?
The code can be compiled
The text was updated successfully, but these errors were encountered: