-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
go/types, types2: enable real
, imag
and complex
with type parameter arguments
#50937
Comments
Change https://golang.org/cl/382116 mentions this issue: |
We can type-check these fine but the API implications are unclear. Fixes #50912. For #50937. Change-Id: If29bbb4a257ff6a85e3bfcd4755fd8f90c80fb87 Reviewed-on: https://go-review.googlesource.com/c/go/+/382116 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Shouldn't that limitation be mentioned in the docs ? 4fea593 only contains code changes. |
@fgm, yes, doc changes are forthcoming. |
Change https://golang.org/cl/381967 mentions this issue: |
For #47694. For #50912. For #50937. Change-Id: I3fae6c8dbbd61a45e669b8fb0c18ac76f2183963 Reviewed-on: https://go-review.googlesource.com/c/go/+/381967 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Creating pseudo TypeParams to represent these derived types seems okay, but feels a bit inconsistent to me with how structural typing works otherwise in Go and the go/types API. If there's no declared name in the source file (i.e., no TypeName), then I think representing it as a TypeParam seems not ideal. Could we instead create new Types to directly represent "type operations"? Like suppose we had types.FloatOf and types.ComplexOf, so that given value I expect we'll need a types.ElemOf operation too, if we allow things like dereferencing pointers of whose type is constrained to |
Just to make sure I'm not missing something: currently where a structural type is used we expose the structural type itself in the API, right? Are you aware of any other conventions? Edit: @mdempsky |
Instead of providing type operations, perhaps we can redefine the built-ins in a backward-compatible way, for instance: func real[Argument ~complex64|~complex128, Result ~float32|~float64](z Argument) Result and record the built-in signature as such. Then all type parameters are declared. |
@findleyr Sorry, when I wrote "structural typing" I meant as opposed to nominal typing (i.e., https://en.wikipedia.org/wiki/Structural_type_system). I didn't mean the Go-specific use case. @griesemer But I think we need a way to establish that Argument and Result are related types? E.g., for
we need to know that for any |
We could make the func real[Argument ~complex64|~complex128, Result real(Argument)](z Argument) Result Such a type function would only be permitted in constraints. |
This sounds similar to the RealOf type operator I described above, but with the extra complexity of representing I also don't think that solution generalizes to allowing dereferencing |
I think that any discussion of how to handle these cases should also consider how to handle cases like func First[T ~string | ~[]byte](s T) int {
for _, c := range s {
return int(c)
}
} Here |
@ianlancetaylor My first proposal would be to have types.RangeKeyOf and types.RangeValueOf type operators, which represent the corresponding types assigned to the RangeStmt.{Key,Value} expressions, respectively. But I think there's also value in trying to consider creating a common Type instance to represent all of these operations, rather than creating one-off Types for each operation. |
I like @mdempsky's idea of type operations/functions Since the shape type implementation may vary between compilers, it may be that thinking of these as an enumerated set of rules is a little better, since the compiler will have to implement the rule. I.e. if it sees a type I definitely don't see any particular use in representing these dependent types as type parameters - but at the very least, they need to have a rule/function associated with them. (Or each compiler will just have to implement these rules independently based on the builtins/constructs being used at that point in the program.) |
Moving to 1.21. Not a high priority. |
#45049 describes a possible solution to this problem by introducing a generic complex type that may be parameterized with |
This will not happen for 1.21. Pushing to next cycle. |
For 1.18 we disabled (report an error) when applying one of these built-in functions to arguments of type parameter type because it involves the creation of a synthetic type parameter which may break
go/types
API invariants if those types leak.Need to investigate what the correct approach is (e.g., should we track the type parameters in the
types.Info.Implicits
map?) and re-enable the code. The compiler may also need minor adjustments.cc: @findleyr
cc: @ianlancetaylor
The text was updated successfully, but these errors were encountered: