You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have been trying out the Golang Generics, and I love them. Great work! However, one thing that I am using a lot in Typescript seems to be missing, or I can not find an answer anywhere. I would love to be able to create a function that accepts a generic struct element with a minimum requirement of a property that needs to be passed to the function. Something like that exists in a Typescript where <T extends {name: string}>
Let's analyze the code below. In the function printNameAndAge, on the place where I put (*extends*) placeholder, I think it would be awesome if somehow we could specify that it is a minimum property of a struct requirement for this function as I will work only with Age and Name
Note: I have seen many workarounds with Interfaces for this, by implementing a function for each property, and all of that seems too messy for such a simple requirement. Once again, I might miss it, or missed the point completely about it, as I have just started exploring Generics in Go, and I am coming from Typescript world.
Not for or against, but I think that a more Go-like syntax would be to make it part of the type set system. For example,
typeNameAndAgeinterface {
~struct {
Name~string// Being able to use ~ on the fields might be overkill...Age~int...// Literal ... in the code to indicate that more fields are allowed. Possibly unnecessary.
}
}
Hi there,
I have been trying out the Golang Generics, and I love them. Great work! However, one thing that I am using a lot in Typescript seems to be missing, or I can not find an answer anywhere. I would love to be able to create a function that accepts a generic struct element with a minimum requirement of a property that needs to be passed to the function. Something like that exists in a Typescript where
<T extends {name: string}>
Let's analyze the code below. In the function
printNameAndAge
, on the place where I put(*extends*)
placeholder, I think it would be awesome if somehow we could specify that it is a minimum property of a struct requirement for this function as I will work only with Age and NameNote: I have seen many workarounds with Interfaces for this, by implementing a function for each property, and all of that seems too messy for such a simple requirement. Once again, I might miss it, or missed the point completely about it, as I have just started exploring Generics in Go, and I am coming from Typescript world.
The text was updated successfully, but these errors were encountered: