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
proposal: Go 2: generics using const struct fields #30518
Comments
This proposal would not allow any generic functions (except for methods on generic struct types), or generic non-struct types. That goes against the principle of orthogonality which Go seeks to follow. There's probably more problems, but this is a big one. |
Interesting observation, I have always thought about orthogonality as being mostly a function of preventing there from being two different ways of accomplishing the same task, rather than making sure that as many permutations of features are usable together as possible. I am curious how important you think it is to have compiletime parameterization based on anything other than types, like integers, first-class functions, or other go types. I considered for a while a simpler version that just allowed One primary goal I was trying to accomplish was to follow the principle of least surprise, ideally to the point of making functionality that someone learning the language today would expect to work already, and be surprised does not exist. Contracts don't feel to me like they rise to that level of intuitiveness, but "fields of structs can be type constants" does. I'm curious what your thoughts are. I think the lack of support for generic standalone functions is not that big of a deal in real large-scale programs, although it looks significant on tiny examples. Especially with the pattern of size-0 structs acting as user-declared "packages" of functions, I'm OK with there being a little more typing required to be explicit about using generic functions, and |
Interesting proposal. Why not also allow const type also in parameters to functions, to get generic functions also? And for the contraints of cont type, interfaces should also be allowed in the constraints block after them. |
@beoran The original intent behind not allowing const arguments to functions was to separate the place where you feed the types into a generic function from the place where you call it. This makes tooling (including compiler messages) simpler, because any function will always have a specific concrete type at the time it is called, and figuring out a valid set of arguments for it doesn't risk becoming a complicated task. It is definitely a little bit verbose, but it felt to me like a little more keyboard-typing would be better than a little more complexity in this case. I'm curious to hear your thoughts, particularly around the pseudo-package pattern used in the Regarding constraint blocks, interfaces would definately be allowed and encouraged in those blocks, both for simple cases and more complicated things like the |
I have seen many Go Generics proposals, and while this has some good ideas, I think it ends up being less powerful than it could be. I implemented something similar to this proposal, using |
@beoran I think we should be careful to not just make generics maximally powerful, in a similar way to the fact that interfaces in Go "only" support methods, not fields or any other properties of types. It requires some more work from the programmer on occasion (like making getters), and is arguably less powerful than an interface syntax that also allowed direct specification of fields, but is also simpler, which is important. I worry that many/all of the more powerful generics systems I have seen proposed are also, by their very nature, hard to reason about what types can be used where (or turing-complete in the worst case). Much like how Go modules are intentionally limited compared to something like dep, I think we need a slightly more limiting generic system at the benefit of significantly simpler behavior. If you are aware of other proposals that fall into that bucket (or that otherwise disallow confusing and complicated type signatures), I would love to hear about them! |
For the sake of having all the discussion in one place, I'm copying here a conversation with @ianlancetaylor from the golang-dev mailing list.
I have also updated the document with a brief bullet point about this issue, since it has come up multiple times. |
Let's see how the current generics design draft, which does permit generic functions, plays out. If that does not get adopted, we can return to this idea. |
I ended up writing and refining a design-document-like-thing for an approach to generics that lives at the intersection of the existing go features of structs and constants in order to more naturally integrate into the language while remaining orthogonal to current features. Any and all suggestions are welcome.
go2 proposal: simple generics using const struct fields
To summarize, the basic idea is to allow struct type declarations to include
const
andconst type
fields, which can be referenced inside their methods to allow generic code. These fields must be filled at the time of variable declaration in a way that allows the compiler and tools to know the concrete signatures of their methods at the callsite.The text was updated successfully, but these errors were encountered: