Cannot instantiate T(ype) passed as pointer T?

98 views
Skip to first unread message

Slawomir Pryczek

unread,
Jul 21, 2022, 2:08:51 AM7/21/22
to golang-nuts
https://gotipplay.golang.org/p/ojY3RRJRJgy

func GenericData[T DATA, T1 any]() {
}

When DATA is interface of pointer type, so methods can modify the underlying structure...

// T1 <- any type, so can instantiate but can't increment as there's no interface
var tt T1 
fmt.Println(tt)

var d T      // T <- DATA interface type, so can't initialize but can instantiate
can't instantiate because the object will never initialize (nil)

Can i create new object of type DATA somehow?

Slawomir Pryczek

unread,
Jul 21, 2022, 2:09:53 AM7/21/22
to golang-nuts
Actually this is better code for the question
https://gotipplay.golang.org/p/GQlB2tyyj53

Axel Wagner

unread,
Jul 21, 2022, 2:25:44 AM7/21/22
to Slawomir Pryczek, golang-nuts
I'm not *entirely* sure what you are asking, but does this help, perchance?

--
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/4b36ca5e-ab1e-4468-85e5-0467efa34ca9n%40googlegroups.com.

Slawomir Pryczek

unread,
Jul 21, 2022, 3:35:35 AM7/21/22
to golang-nuts
Thanks, actually that's what I was looking for.  Don't have much experience using generics (like 2 days) but looks rather inconvinient to do that. This pattern is convinient for you and you're using it in your code? You think there are plans to introducing some feature which will allow to work with *interfaces & generics "normally" (without additional boilerplate?)

Axel Wagner

unread,
Jul 21, 2022, 4:04:21 AM7/21/22
to Slawomir Pryczek, golang-nuts
The design doc says this:
What we need is a way to say that the type constraint applies to either the pointer method set or the value method set. The body of the function would be required to only call the method on addressable values of the type.

It's not clear how often this problem comes up in practice.
 
So, it's on the radar, but it's not clear if this will happen - and if so, when.

Reply all
Reply to author
Forward
0 new messages