Macros for Typedefs

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Macros for Typedefs

Heinz Hölzer-2
Hi List,

it would be nice to generate a Typedef with the Help of Macros.

This would make it possible to generate the structure of a type and pass it around in your program. There is currently no way to store an object (returned from a macro) in a typesafe way.

For example:

class Test {
    public static function getObj () // i can only set Dynamic as a return type
    {
        var o = MyMacro.generateObjectFromXml("my.xml");
        return o;
    }
}

but if i can store the typedefinition in a typedef this would be possible:

typedef MyXmlType = MyMacro.generateTypedefFromXml("my.xml");

class Test {
    public static function getObj ():MyXmlType
    {
        var o = MyMacro.generateObjectFromXml("my.xml");
        return o;
    }
}



Here are some ideas how this could be implemented:

typedef MyType = MyMacro.getMyType();

or

@:build(MyMacro.getMyType())
typedef MyType;

or

typedef MyType = haxe.macro.Macro<MyMacro.getMyType()>;

where the Macro must return a valid Typedefinition.

It should also be possible to return Type Parameters from the Macro, so this should be possible:

typedef MyType<T,A> = MyMacro.getType();

The Macro should be executed and the result should be checked against the left-side.

what do you think?




--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Macros for Typedefs

Heinz Hölzer-2

For example:

class Test {
    public static function getObj () // i can only set Dynamic as a return type
    {
        var o = MyMacro.generateObjectFromXml("my.xml");
        return o;
    }
}


This was not the best example because i can leave the Return Type and the Compiler can infer it. A better example would be this:


class Test {
    public static function getConfig () // i can only set Dynamic as a return type
    {
        var o = MyMacro.generateObjectFromXml("my.xml");
        return o;
    }
}

class Foo {
   
// i cannot specify the type for config (only Dynamic)
    var config;
    public function new (config)
    {
        this.config = config;
    }
   
}

class Main {

public static function main () {
    var config =
Test.getConfig();
    var foo = new Foo(config);
   
}


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Macros for Typedefs

Jordo Odroj
Heinz, I don't want your question to be overlooked. I, myself have a need for generating typedefs based on a Macro. Is there a way to accomplish this?]

2011/2/15 Heinz Hölzer <[hidden email]>

For example:

class Test {
    public static function getObj () // i can only set Dynamic as a return type
    {
        var o = MyMacro.generateObjectFromXml("my.xml");
        return o;
    }
}


This was not the best example because i can leave the Return Type and the Compiler can infer it. A better example would be this:


class Test {
    public static function getConfig () // i can only set Dynamic as a return type

    {
        var o = MyMacro.generateObjectFromXml("my.xml");
        return o;
    }
}

class Foo {
   
// i cannot specify the type for config (only Dynamic)
    var config;
    public function new (config)
    {
        this.config = config;
    }
   
}

class Main {

public static function main () {
    var config =
Test.getConfig();
    var foo = new Foo(config);
   
}


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Macros for Typedefs

Nicolas Cannasse
Le 17/02/2011 06:59, Jordo Odroj a écrit :
> Heinz, I don't want your question to be overlooked. I, myself have a
> need for generating typedefs based on a Macro. Is there a way to
> accomplish this?]

Not right now, but this should be easily doable, Heinz please send a
feature request on http://code.google.com/p/haxe/issues

Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Macros for Typedefs

Heinz Hölzer-2
In reply to this post by Jordo Odroj
There's already a dirty way to accomplish that:

// TypedefMacro.hx

package ;

#if macro
import haxe.macro.Context;
import haxe.macro.Expr;
import neko.FileSystem;
import neko.io.File;
#end

class TypedefMacro {
    @:macro public static function makeTypedef (name:Expr):Expr
    {
        var typeName = switch (name.expr) {
            case EConst(c): switch (c) {
                case CString(s): s;
                default: Context.error("Const String expected", name.pos);
            }
            default: Context.error("Const String expected", name.pos);
        }
        var fileName = typeName + ".hx";
        if (FileSystem.exists(fileName)) Context.error("file " + fileName + " exists already", name.pos);
        var file = File.write(fileName, false);
        file.writeString("typedef " + typeName + " = { name:String, age:Int};");
        file.close();
        Context.getType(typeName);
        FileSystem.deleteFile(fileName);
       
        return { expr: EBlock([]), pos:name.pos };
    }
}

// Test.hx

package ;


@:build(TypedefMacro.makeTypedef("MyName"))
extern class Unused {}

typedef S = MyName;

class Test
{

    public static function main()
    {
        var s:S = { age:5, name:"tim"};
       
        trace(s);
    }
   
}


Am 17.02.2011 06:59, schrieb Jordo Odroj:
Heinz, I don't want your question to be overlooked. I, myself have a need for generating typedefs based on a Macro. Is there a way to accomplish this?]

2011/2/15 Heinz Hölzer <[hidden email]>

For example:

class Test {
    public static function getObj () // i can only set Dynamic as a return type
    {
        var o = MyMacro.generateObjectFromXml("my.xml");
        return o;
    }
}


This was not the best example because i can leave the Return Type and the Compiler can infer it. A better example would be this:


class Test {
    public static function getConfig () // i can only set Dynamic as a return type

    {
        var o = MyMacro.generateObjectFromXml("my.xml");
        return o;
    }
}

class Foo {
   
// i cannot specify the type for config (only Dynamic)
    var config;
    public function new (config)
    {
        this.config = config;
    }
   
}

class Main {

public static function main () {
    var config =
Test.getConfig();
    var foo = new Foo(config);
   
}


--
haXe - an open source web programming language
http://haxe.org



--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Macros for Typedefs

Heinz Hölzer-2
In reply to this post by Nicolas Cannasse
done ;)

Am 17.02.2011 10:03, schrieb Nicolas Cannasse:

> Le 17/02/2011 06:59, Jordo Odroj a écrit :
>> Heinz, I don't want your question to be overlooked. I, myself have a
>> need for generating typedefs based on a Macro. Is there a way to
>> accomplish this?]
>
> Not right now, but this should be easily doable, Heinz please send a
> feature request on http://code.google.com/p/haxe/issues
>
> Nicolas
>


--
haXe - an open source web programming language
http://haxe.org