proposal: constraints: new package to define standard type parameter constraints (discussion) #47319
Replies: 9 comments 49 replies
-
[Resolved: Decided not to do this, at least not yet. - rsc, July 28 2021] On the issue, I had mentioned that another useful type constraint would be "Arithmetic", which contained any type with |
Beta Was this translation helpful? Give feedback.
-
[Resolved: Decided to keep "constraints". - rsc, July 28 2021] I'd prefer the singular form |
Beta Was this translation helpful? Give feedback.
-
[Resolved: Will make docs clear and add a vet check. - rsc, July 28 2021] Is adding types in the future going to cause compatibility problems? (This is reposting and merging two of my comments on the issue.) A simple but perhaps too contrived example: type MySigned interface { type int8, int16, int32, int64 }
func MyAbs[T MySigned](v T) T { ... }
func Abs[T constraints.Signed](v T) T { return MyAbs(v) } (Of course presumably A perhaps more likely example (assuming #45346 is accepted): func Abs[T constraints.Float](v T) v {
switch T {
case ~float32:
return math.Float32FromBits(math.Float32Bits(v) &^ (1 << 31))
case ~float64:
return math.Float64FromBits(math.Float64Bits(v) &^ (1 << 63)) // math.Abs
default:
panic("unknown type")
}
} (I'm using syntax proposed in #45380 here for brevity, but since It doesn't seem like this should block the addition of the |
Beta Was this translation helpful? Give feedback.
-
I have a question about |
Beta Was this translation helpful? Give feedback.
-
Update, July 28 2021 Based on the discussion, we will make the following changes to the proposed API:
I have also added [Resolved] notes at the top of comments that we consider resolved, in addition to replying with text like "Resolved...". @ianlancetaylor is going to update the proposal text. |
Beta Was this translation helpful? Give feedback.
-
Given that we are adding |
Beta Was this translation helpful? Give feedback.
-
Update, August 4 2021 Based on the discussion, we will make the following changes to the proposed API:
@ianlancetaylor is going to update the proposal text. I am also starting to wonder how long it will take me to learn to type the word constraints. |
Beta Was this translation helpful? Give feedback.
-
Presumably, the |
Beta Was this translation helpful? Give feedback.
-
I believe most functions accepting channels should restrict the direction of communication. The We could add |
Beta Was this translation helpful? Give feedback.
-
We are ready to finish the discussion on #45458, because type sets have been accepted.
Here is the current API:
The intent is that if we add any new predeclared types, such as
int128
anduint128
, those types will be supported by these constraints as appropriate based on the constraint descriptions.Beta Was this translation helpful? Give feedback.
All reactions