Open mark-rushakoff opened 2 months ago
Related Issues and Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
CC @griesemer
I think we would need a special type inference rule for channel types.
Note that I think that your program isn't going to work anyhow. The cap(ch)
call isn't permitted, though that would be fixed by #63940.
Adjusting the code to remove the lines that failed compilation, it does appear that cap(ch)
is reported correctly, on go1.23. https://go.dev/play/p/jS1_QIeyrUF
It looks like len(ch)
works too.
What @ianlancetaylor said. The issue here is that there's no core type for the constraint. At the moment inference does work if the constraint was just chan T | <-chan T
or chan T | chan<- T
(there's a core type) but instantiation doesn't work of course for the one direction that's not in the constraint.
This might also be addressed by #63940.
Marking for 1.24 - not promising that this will be addressed, but so I keep an eye on it while investigating #63940.
Moving to 1.25.
Go version
go1.23 and go tip
Output of
go env
in your module/workspace:What did you do?
https://go.dev/play/p/-psvliJDE_j
I want to write a small utility that takes a specific action that is determined by a channel's length and/or capacity. In this codebase, there are a general mix of directional and bidirectional channels. I thought I would be able to write a constraint such that my helper can accept a channel of any directionality. And I can indeed write that constraint, but when I call a function with that constraint, I have to provide the channel's element type as a type parameter in order to get the program to compile.
I am surprised that I have to provide the channel's element type, but maybe I am missing something about constraints or generics.
What did you see happen?
What did you expect to see?
I expected that
printCap(ch)
would compile; that I would not have to writeprintCap[int](ch)
.I tried searching the issues for variations of "constraints", "channels", and "direction" or "directionality" but I was not able to find any issues that looked similar.