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

Need something like @:genericBuild for generic functions #3001

Closed
kevinresol opened this Issue May 16, 2014 · 2 comments

Comments

Projects
None yet
2 participants
@kevinresol
Contributor

kevinresol commented May 16, 2014

@:generic
public function clone<T>(object:T):T
{
    Macro.buildThisFunction();
}

Macro.buildThisFunction(); is a macro function to be executed per-instance like @:genericBuild, where the type parameter should be accessible.

Edit: For those who come across this post looking for a solution, here is one: http://www.kevinresol.com/2016-11-23/genericbuild-function-haxe

@Simn

This comment has been minimized.

Member

Simn commented May 17, 2014

It's an interesting idea, but there are quite a few questions we would have to resolve first.

For starters, what would the macro return? The reason @:genericBuild works nicely for types is because the macro can add a new type to the context (using Context.defineType) and then return a reference to it. This is not possible at field level because non-build macros cannot inject fields into the class.

In summary this is not something we can address at the moment, so I'll close the issue for now.

@Simn Simn closed this May 17, 2014

@kevinresol

This comment has been minimized.

Contributor

kevinresol commented May 17, 2014

@Simn Thanks Simon. Is that because the macros (both build macros and non-build macro) are executed before the typing phase?

I have two ideas but not sure if they works

1. Field-level build macros

@:build(someBuildFunction()) //or @:genericBuild
public function clone<T>(object:T):T {}

someBuildFunction() could possibly return:

  • a Field to replace the function-to-be-built; or
  • an Expr to become the contents of the function-to-be-built

2. Enhance current @:genericBuild so the macro can pick the type parameters in a generic function

@:genericBuild
class Cloner<T> {}

class Main
{
    @:generic
    public static function clone<T>(object:T) 
    {
        new Cloner<T>();
    }

    static function main()    
    {
        clone(new ObjectA());
        clone(new ObjectB());
    }
}

Currently the generic build macro only being called once for T. It would be nice if it could be called twice, for ObjectA and ObjectB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment