Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic Functions Cause Translation Failure in Go2hx #178

Open
Yeaseen opened this issue Oct 29, 2024 · 2 comments
Open

Generic Functions Cause Translation Failure in Go2hx #178

Yeaseen opened this issue Oct 29, 2024 · 2 comments
Labels
2nd order problem Implement full language subspec

Comments

@Yeaseen
Copy link

Yeaseen commented Oct 29, 2024

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.

@elliott5
Copy link
Collaborator

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.

@Yeaseen
Copy link
Author

Yeaseen commented Oct 30, 2024

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.

@PXshadow PXshadow added the 2nd order problem Implement full language subspec label Nov 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2nd order problem Implement full language subspec
Projects
None yet
Development

No branches or pull requests

3 participants