Skip to content

cmd/go2go: adding strings together is not defined #39623

@aykevl

Description

@aykevl

I've been experimenting a bit with generics at https://go2goplay.golang.org/ and I think I found a bug.

What did you do?

See: https://go2goplay.golang.org/p/efS6x6s-9NI
I tried concatenating a string:

type IntString interface {
	type int, string
}

func Sum(type T IntString)(s []T) (result T) {
	for _, v := range s {
		result += v // this fails even though it's possible to add strings together
	}
	return
}

What did you expect to see?

Strings can be added (concatenated) together, so I would expect it to also work when using generics.
Specialized variant of the same function, which does compile:

func SumString(s []string) (result string) {
	for _, v := range s {
		result += v
	}
	return
}

What did you see instead?

It reports the following error:

type checking failed for main
prog.go2:13:3: invalid operation: operator + not defined for result (variable of type T)

Both integers and strings support the + operator so I would expect this to work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions