Implement generic type interface

154 views
Skip to first unread message

Kaisa Lim

unread,
Jan 24, 2022, 12:02:09 PM1/24/22
to golang-nuts
Before the question, I'm sorry I'm not good at English

when go 1.18 beta released, i try to test generic type 
Most of the code worked, but something went wrong when implementing the generic interface.

This is my code >
type Repository[T any] interface {
    Save(T) T
}

type repo[T any] struct {
}

func NewRepository[T any]() Repository[T] {
    return &repo[T]{}
}

func (r repo[T]) Save(entity T) T {
    return entity
}

This code looks fine and works normally when executed.
But, ide throw compile exception. Why?

스크린샷 2022-01-24 오후 7.12.19.png

Again, build and run is normal *

Axel Wagner

unread,
Jan 24, 2022, 12:05:27 PM1/24/22
to golang-nuts
It seems you might want to read the actual error message it shows. And perhaps contact the vendor of that IDE (you didn't say which one).

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/19fbde0e-d3e3-46c9-b727-1833a1b0bf1dn%40googlegroups.com.

Ian Lance Taylor

unread,
Jan 24, 2022, 5:48:19 PM1/24/22
to Kaisa Lim, golang-nuts
What IDE are you using?

Generics are very new and are not yet in any released version of Go.  Are you sure that your IDE supports generics?

Ian

Denis Cheremisov

unread,
Jan 26, 2022, 1:51:55 AM1/26/22
to golang-nuts
GoLand' support for generics is in alpha stage today, false negatives are pretty common and this is your case.

понедельник, 24 января 2022 г. в 20:02:09 UTC+3, ka...@swit.io:
Reply all
Reply to author
Forward
0 new messages