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

cmd/go2go: generic type receiver cannot have type parameters in the method declaration? #39704

Closed
ycalansy opened this issue Jun 19, 2020 · 1 comment

Comments

@ycalansy
Copy link

package main

import (
	"fmt"
)

func main() {
	vec := Vector(string){"abc", "xyz"}

	vec.ForEach(PrintString)
	
	fmt.Println(Reduce(int, string)(0, vec, CountCharacters))

	fmt.Println(vec.Reduce(int)(0, CountCharacters))
}

func PrintString(s string) { fmt.Println(s) }

func CountCharacters(acc int, s string) int { return acc + len(s) }

type Vector(type E) []E

func (v Vector(E)) ForEach(fn func(E)) {
	for _, ele := range v {
		fn(ele)
	}
}

func (v Vector(E)) Reduce(type A)(acc A, fn func(A, E) A) A {
	for _, ele := range v {
		acc = fn(acc, ele)
	}
	return acc
}

func Reduce(type A, E)(acc A, vec Vector(E), fn func(A, E) A) A {
	for _, ele := range vec {
		acc = fn(acc, ele)
	}
	return acc
}

What did you expect to see?

abc
xyz
6
6

What did you see instead?

type checking failed for main
prog.go2:29:26: methods cannot have type parameters

@ycalansy ycalansy changed the title cmd/go2go: Generic type receiver cannot have type parameters method? cmd/go2go: generic type receiver cannot have type parameters method? Jun 19, 2020
@ycalansy ycalansy changed the title cmd/go2go: generic type receiver cannot have type parameters method? cmd/go2go: generic type receiver cannot have type parameters in the method declaration? Jun 19, 2020
@golang golang locked and limited conversation to collaborators Jun 19, 2021
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

3 participants