Hacker News new | past | comments | ask | show | jobs | submit login

so does this mean there are no generic structs in generic go?



You can do

    type X[T any] struct{}
    func (x *X[T]) Method() {}
just fine. What you can't do is

    func (x *X[T]) Method[T2 any]() {}


Not saying mixing OO and generics could never have any merit, but.. Isn't a method just a function having an object as first parameter. Does Go change this beyond "syntactic sugar" somehow? Been a while from coding Go, so interested to hear.

The rationale seems to me that generics be functions first (ok, procedural), and not complecting it with objects and OO too much, whatever that mix could mean..


> Isn't a method just a function having an object as first parameter.

Differences:

- Methods must be defined in the same package as the receiver.

- Methods can be used to implement interfaces.

- Methods can be discovered dynamically by inspecting the type of the receiver (either through reflection or with a dynamic cast).


In other words, methods cannot introduce generic types.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: