Re: [go-nuts] [generics] type constraint for structs

2024-04-04 Thread atd...@gmail.com
It allows to return the value pointed at (*Element) but I still don't have access to the *Element field adddress of let's say var d Div so can't change d.Element to swap it with another value. (by taking Ref(,e) where e is some *Element) It's usable as a constraint but that's not sufficient.

Re: [go-nuts] [generics] type constraint for structs

2024-04-03 Thread 'Axel Wagner' via golang-nuts
How does `interface{ AsElement() *Element }` not do exactly what you want? On Thu, Apr 4, 2024 at 4:14 AM atd...@gmail.com wrote: > Might have come across this today as I was trying to simplify some code. > > Basically, I have a base type called *Element that has a method > AsElement() *Element

Re: [go-nuts] [generics] type constraint for structs

2024-04-03 Thread atd...@gmail.com
Ah, I forgot to use reflection for instance... That might be doable. On Thursday, April 4, 2024 at 4:13:35 AM UTC+2 atd...@gmail.com wrote: > Might have come across this today as I was trying to simplify some code. > > Basically, I have a base type called *Element that has a method >

Re: [go-nuts] [generics] type constraint for structs

2024-04-03 Thread atd...@gmail.com
Might have come across this today as I was trying to simplify some code. Basically, I have a base type called *Element that has a method AsElement() *Element that returns itself. And this base element is used as a base for many others, for isntance: type Div struct{ *Element} type Span

Re: [go-nuts] [generics] type constraint for structs

2024-04-03 Thread Adam Manwaring
While this would make some things much easier for me, it seems this would be a pretty fundamental change. Constraints are essentially interfaces and interfaces in Go are defined by behaviors. Structs, on the other hand, are defined by properties. There is no behavior that all structs have that

Re: [go-nuts] [generics] type constraint for structs

2024-03-27 Thread Makis Maropoulos
Same here @Abraham, ResponseType interface { ~struct{} } Obviously this doesn't work, I would love to see it working though. On Wednesday 14 September 2022 at 17:48:19 UTC+3 Abraham wrote: > I am glad I found this thread because I was just now breaking my head > figuring out

Re: [go-nuts] [generics] type constraint for structs

2022-09-14 Thread Abraham
I am glad I found this thread because I was just now breaking my head figuring out why my was not working On Wednesday, May 18, 2022 at 10:41:29 PM UTC-4 Ian Lance Taylor wrote: > On Wed, May 18, 2022 at 7:36 PM Jeremy Kassis wrote: > > > > Where exactly did this land? Seems like an

Re: [go-nuts] [generics] type constraint for structs

2022-05-18 Thread Ian Lance Taylor
On Wed, May 18, 2022 at 7:36 PM Jeremy Kassis wrote: > > Where exactly did this land? Seems like an important conversation... To date there is no way to write a constraint that requires that a type argument be a struct type. > ``` > // RPCHandler passes RPCReq and RPCRes as fn args > func

Re: [go-nuts] [generics] type constraint for structs

2022-05-18 Thread Jeremy Kassis
Where exactly did this land? Seems like an important conversation... ``` // RPCHandler passes RPCReq and RPCRes as fn args func RPCHandler[T RPCReq, S RPCRes](fn func(T, S)) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { req := T{} if err :=

Re: [go-nuts] [generics] type constraint for structs

2020-06-24 Thread Ian Lance Taylor
On Wed, Jun 24, 2020 at 4:57 PM Pee Jai wrote: > > Just to clarify: This is not directly related to generics. It is related to > interfaces (and the type constraints feature) which are incidental to > generics and provides extra power to developers. In this particular use case, > it provides a

Re: [go-nuts] [generics] type constraint for structs

2020-06-24 Thread Robert Engels
But if you interpret the method as ‘format this struct’ a struct has named fields. A primitive has no named fields so the output is empty. It is almost always easier to define the contract if the method in an unbreakable way than rely on the compiler to catch all problems. For example what

Re: [go-nuts] [generics] type constraint for structs

2020-06-24 Thread Pee Jai
Just to clarify: This is not directly related to generics. It is related to interfaces (and the type constraints feature) which are incidental to generics and provides extra power to developers. In this particular use case, it provides a useful compile-time check for an argument that is

Re: [go-nuts] [generics] type constraint for structs

2020-06-24 Thread Pee Jai
I disagree. A type constraint for a struct would definitely provide compile-time guarantee that only a struct can be used. On Thursday, June 25, 2020 at 8:18:13 AM UTC+10, Ian Lance Taylor wrote: > > On Wed, Jun 24, 2020 at 12:22 PM David Finkel > wrote: > > > > On Wed, Jun 24, 2020 at 9:34

Re: [go-nuts] [generics] type constraint for structs

2020-06-24 Thread Ian Lance Taylor
On Wed, Jun 24, 2020 at 12:22 PM David Finkel wrote: > > On Wed, Jun 24, 2020 at 9:34 AM Robert Engels wrote: >> >> Generics will not solve this fully anyway - you’ll still need reflection. >> Why not just return empty if the user doesn’t pass a struct? >> > That sounds like a bug waiting to

Re: [go-nuts] [generics] type constraint for structs

2020-06-24 Thread Robert Engels
Generics will not solve this fully anyway - you’ll still need reflection. Why not just return empty if the user doesn’t pass a struct? > On Jun 24, 2020, at 8:29 AM, Pee Jai wrote: > >  > Here is my use case: > https://godoc.org/github.com/rocketlaunchr/react#UnmarshalState > > That

Re: [go-nuts] [generics] type constraint for structs

2020-06-24 Thread Pee Jai
Here is my use case: https://godoc.org/github.com/rocketlaunchr/react#UnmarshalState That function has an argument that accepts only a struct. I use the reflect package to iterate over the structs fields (and also to check if it actually is a struct). The function has notapplicability for

Re: [go-nuts] [generics] type constraint for structs

2020-06-23 Thread Ian Lance Taylor
On Mon, Jun 22, 2020 at 7:08 PM Pee Jai wrote: > > I can't find a way to constrain for just structs. I don't want primitives. The design draft doesn't provide any mechanism for a type constraint to match any arbitrary struct type, and not any other kind of type. But it's not clear why that

[go-nuts] [generics] type constraint for structs

2020-06-22 Thread Pee Jai
I can't find a way to constrain for just structs. I don't want primitives. -- 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