chengxilo commented on code in PR #1995:
URL: https://github.com/apache/iggy/pull/1995#discussion_r2201170917
##########
foreign/go/contracts/identifier.go:
##########
@@ -30,22 +32,45 @@ const (
StringId IdKind = 2
)
-func NewIdentifier(id any) Identifier {
- var kind IdKind
- var length int
-
- switch v := id.(type) {
- case int:
- kind = NumericId
- length = 4
- case string:
- kind = StringId
- length = len(v)
+// NewNumericIdentifier creates a new identifier from the given numeric value.
+func NewNumericIdentifier(value uint32) (Identifier, error) {
Review Comment:
Will change my code like this. But I am not going to use `~uint32 |
~string`. Instead, I will use `uint32 | string`.
There is some explaination about the potential problem with ~xxxx in generic.
https://go.googlesource.com/proposal/+/master/design/43651-type-parameters.md#identifying-the-matched-predeclared-type
I also have a small example here:
```go
package main
import "fmt"
type MyUint32 uint32
func TPrint[T ~uint32 | ~string](val T) {
switch any(val).(type) {
case uint32:
fmt.Printf("uint32: %v", val)
case float64:
fmt.Printf("float64: %v", val)
default:
fmt.Printf("other: %v", val)
}
}
func main() {
TPrint(MyUint32(42))
}
```
But overall it's a good idea.
I tried to use the generic in the Identifier struct. However, I gave up
since it would not compile because methods are not allowed to have generic type
params. Using generic in this function can be a good choice.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]