Yes:
Tilde ("~") in type constraints
In Go, generic types can have constraints (this is why we swapped wrapCType[CTYPE any, ...] to wrapCType[CTYPE CNumber, ...]) which are defined as interfaces. When you write something like:
type Integer interface {
int | int8 | int16 | int32 | int64
}
func add[T Integer](a, b T) T { return a + b }
The Integer constraint says that the type, T, must be an int, int8, etc.
However, if you do this:
type MyInt int
var a MyInt = 1
var b MyInt = 2
c := add(a, b)
It won’t work because a and b are of type MyInt, not int, which isn’t in the constraint list.
The tilde (“~”) in a constraint like ~int means that the type doesn’t need to be int exactly; it can be any type whose underlying type is int (such as MyInt in my example).
For some background...
The “C” package you get when you write import "C" isn’t a “real” package like, for example, the “fmt” package is. It’s a “virtual” package that tells the Go compiler to do fancy stuff to “translate” between Go and C conventions (e.g. the compiler does something different when calling a Go function vs. calling a C function, etc.). I’m not super familiar with cgo, so I wasn’t sure what kind of “magic” the compiler uses for the C.int, C.char, etc. types. Based on that error message, it looks like C.char gets translated to an internal _Ctype_char type and that type’s underlying type is int8 (so it’s like somewhere in Cgo it says type char = _Ctype_char and type _Ctype_char = int8).
–
Anyway, based on this error:
underlying type of _Ctype_char is int8
and the other ones like, it looks like you don’t need the CNumber interface and it really all boils down to GoNumber, so you could just say:
func wrapCType[CTYPE, GOTYPE GoNumber](goValue *GOTYPE) (wrapped *CTYPE, finisher func())
At this point, though, I’m not sure what to suggest because I’m not sure if all of the conversions between all of these types are allowed. Can you show an example of how you would use this wrapCType function? Maybe I or someone else will have better suggestions after seeing what you’re trying to do.