[generics] generic method parameters

105 views
Skip to first unread message

Tong Sun

unread,
Apr 30, 2022, 4:43:31 PM4/30/22
to golang-nuts
I've done many reading on Go generic method parameters, and it seems somewhat supported, like from


A generic Get() & Set() can be defined and used:

----------------------------
// Set sets the value.
func (o *Optional[T]) Set(v T) {
   o.value = &v
}

... 

Now the user of my library could write code like this:

o := New(42)
v, err := o.Get()
----------------------------

However, I was having a hard time trying to apply that to my following code:

Is it possible to get its generic Get() & Set() function working (which is currently commented out)?

Thanks

Vasko Zdravevski

unread,
Apr 30, 2022, 4:56:44 PM4/30/22
to golang-nuts
Here is a working version of the Get & Set methods without changing your method signatures. https://go.dev/play/p/MAqFwVINvd8 

Tong Sun

unread,
Apr 30, 2022, 7:55:09 PM4/30/22
to golang-nuts
Thanks a lot sir!
Reply all
Reply to author
Forward
0 new messages