chaokunyang commented on code in PR #3064:
URL: https://github.com/apache/fory/pull/3064#discussion_r2637245287


##########
go/fory/union.go:
##########
@@ -0,0 +1,315 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package fory
+
+import (
+       "fmt"
+       "reflect"
+)
+
+// Union represents a tagged union type that can hold one of several 
alternative types.
+// It's equivalent to Rust's enum, C++'s std::variant, or Python's 
typing.Union.
+//
+// The Value field holds the actual value, which must be one of the types 
specified
+// when registering the Union type.
+//
+// Example usage:
+//
+//     // Create a union that can hold int32 or string
+//     union := fory.Union{Value: int32(42)}
+//     // or
+//     union := fory.Union{Value: "hello"}
+type Union struct {

Review Comment:
   Since we are use go 1.23+, we could use go generics now. We can define union 
as:
   ```go
   package union
   
   import "fmt"
   
   // --- Union2 ---
   
   type Union2[T1 any, T2 any] struct {
       v1 *T1
       v2 *T2
       index int 
   }
   
   func NewUnion2A[T1 any, T2 any](t T1) Union2[T1, T2] {
       return Union2[T1, T2]{v1: &t, index: 1}
   }
   
   func NewUnion2B[T1 any, T2 any](t T2) Union2[T1, T2] {
       return Union2[T1, T2]{v2: &t, index: 2}
   }
   
   func (u Union2[T1, T2]) Match(
       case1 func(T1), 
       case2 func(T2),
   ) {
       switch u.index {
       case 1:
           case1(*u.v1)
       case 2:
           case2(*u.v2)
       default:
           panic("Union2 is uninitialized")
       }
   }
   
   // --- Union3 ---
   
   type Union3[T1 any, T2 any, T3 any] struct {
       v1 *T1
       v2 *T2
       v3 *T3
       index int
   }
   
   func NewUnion3A[T1 any, T2, T3 any](t T1) Union3[T1, T2, T3] {
       return Union3[T1, T2, T3]{v1: &t, index: 1}
   }
   
   func NewUnion3B[T1, T3 any, T2 any](t T2) Union3[T1, T2, T3] {
       return Union3[T1, T2, T3]{v2: &t, index: 2}
   }
   
   func NewUnion3C[T1, T2 any, T3 any](t T3) Union3[T1, T2, T3] {
       return Union3[T1, T2, T3]{v3: &t, index: 3}
   }
   
   func (u Union3[T1, T2, T3]) Match(f1 func(T1), f2 func(T2), f3 func(T3)) {
       switch u.index {
       case 1: f1(*u.v1)
       case 2: f2(*u.v2)
       case 3: f3(*u.v3)
       }
   }
   ```



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to