go2hx icon indicating copy to clipboard operation
go2hx copied to clipboard

Generic Functions Cause Translation Failure in Go2hx

Open Yeaseen opened this issue 1 year ago • 3 comments

Go2hx fails to translate Go code that uses generic functions with type parameters. When attempting to compile a program that uses generics, Go2hx produces an error indicating that the generic type cannot be found. Below is an example of code that works correctly in Go but fails in Go2hx.

Source Go code:

package main
import "fmt"
func main() {
    f[int]()
}
func f[T any]() {
    if []T(nil) != nil {
        fmt.Println("Slice is not nil")
    } else {
        fmt.Println("Slice is nil")
    }
    if (func() T)(nil) != nil {
        fmt.Println("Function is not nil")
    } else {
        fmt.Println("Function is nil")
    }
}

Expected Go Output:

Slice is nil
Function is nil

Translated Haxe Code:

package _internal;
function main():Void {
    _f((0 : stdgo.GoInt));
}
macro function _f<T_>(__generic__0:haxe.macro.Expr.ExprOf<T_>):haxe.macro.Expr.ExprOf<Void>;

Error Message in Haxe:

_internal/Runner.macro.hx:16: character 35 : Type not found : T_

Comment:

The error indicates that Go2hx does not handle generic type parameters (T) correctly. When translating the Go function f[T any](), Go2hx seems unable to define or pass the generic type T to the Haxe equivalent, leading to a Type not found error. Simplifying the Go code to remove generics (i.e., by replacing T with a concrete type such as int) allows the translation to succeed, indicating the issue is specific to generics.

Yeaseen avatar Oct 29 '24 04:10 Yeaseen

Thank you for this @Yeaseen - you are correct, this aspect of Go has not yet been implemented within go2hx.

It is a live issue for the project.

elliott5 avatar Oct 29 '24 06:10 elliott5

Thank you for confirming this! @elliott5 I understand it’s a challenging aspect of Go to implement. I’m happy to help test this wonderful project.

Yeaseen avatar Oct 30 '24 02:10 Yeaseen

This is still an ongoing issue #261 point 2

PXshadow avatar Jul 18 '25 14:07 PXshadow