-
Notifications
You must be signed in to change notification settings - Fork 262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to implement generics? #229
Comments
A fundamental issue came to my mind: But generic code in third-party libraries, which are currently compiled with Go toolchain and then loaded in gophernotes as shared libraries, pose a much more difficult challenge: The issue is easier to understand on a concrete example: type Stack[T any] {
// unexported fields
}
func (s *Stack[T]) push(T elem) {
// ...
}
func (s *Stack[T]) pop() T {
// ...
} when you type If Instead our And here comes the issue: when There's no way to know in advance the instantiations a user may ask for. So there's no way to compile them in advance. This leaves us with two alternative solutions - and I am tempted to call them workarounds rather than solutions, because both have significant drawbacks:
Ideas for other solutions? |
@dwhitena @SpencerPark @sbinet any idea or suggestion on the best approach to move forward ? |
I don't remember whether gomacro is using reflect under the hood to manipulate types, but if it does, one could perhaps weigh in and propose to have a reflect API that can also manipulate parametrized types. |
Yes, gomacro heavily uses This has several limitations, including that Generics are not supported by And I am quite sure there's no plan to support instantiating generics at runtime - which for the foreseeable future will require invoking the Go toolchain. |
A third solution came to my mind: when importing a package, instantiate its generics on a custom type that satisfies the constraint(s). First example: for the Second example: for an hypothetical generic type type Set[T Ordered] { /* ... */ } where the constraint
we can instantiate, compile and load with Go toolchain the type type OrderedX {
X_ interface{} // the wrapped value
Compare_ func(other OrderedX) int
}
// implement the interface Ordered[OrderedX]
func (obj OrderedX) Compare(other OrderedX) int {
return obj.Compare_(other)
} This allows wrapping any type with a method |
Now that Go generics have been approved, we should support them in gophernotes too
More specifically, the underlying interpreter gomacro has an open issue cosmos72/gomacro#24 to track their implementation.
Help is welcome :)
The text was updated successfully, but these errors were encountered: