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

[macro] structure extension information is not available after typing #3198

Closed
nadako opened this issue Jul 22, 2014 · 15 comments
Closed

[macro] structure extension information is not available after typing #3198

nadako opened this issue Jul 22, 2014 · 15 comments
Labels
platform-macro Everything related to Haxe macros

Comments

@nadako
Copy link
Member

nadako commented Jul 22, 2014

Typer currently seems to generate a new TAnonymous for a TExtend, merging its fields and losing information about extended types. However, I find that info useful sometimes and would love to see it available somehow (maybe as metadata or AnonStatus param). For example:

In our game we have type definitions for game content JSON structures like that:

typedef MapItemDef = {
    name:String
}

typedef DragonDef = {
    >MapItemDef,
    elements:Int
}

What I want to do is macro-generate some C# classes for the Unity3D client that wraps those structures and I want to preserve inheritance hierarchy for those classes:

class MapItemDef {
    public readonly string Name;

    public MapItemDef(JSValue o) {
        Name = o["name"].AsString;
    }
}

class DragonDef : MapItemDef {
    public readonly int Elements;

    public DragonDef (JSValue o) : base(o) {
        Elements = o["elements"].AsInt;
    }
}
@ncannasse
Copy link
Member

Suggestion : store it in some metadata ?

@ncannasse
Copy link
Member

Or more exactly : let the compiler store it into some metadata that you can later retreive

@nadako
Copy link
Member Author

nadako commented Jul 22, 2014

Yeah that's one option as I said. I'll try to implement it.

@nadako
Copy link
Member Author

nadako commented Jul 22, 2014

Oh wait, but tanon currently can't have any metadata.

@waneck
Copy link
Member

waneck commented Jul 22, 2014

Maybe create a new TType to that tanon then?

@nadako
Copy link
Member Author

nadako commented Jul 22, 2014

Meh, i don't know, this is not really a typedef and people may rely on it being plain TAnonymous in their macros.

@ncannasse
Copy link
Member

Ah right. We could have it into the a_status then

@nadako
Copy link
Member Author

nadako commented Jul 22, 2014

@ncannasse do you think it will make sense to just add a_extends : t list and don't mess with a_status?

@nadako
Copy link
Member Author

nadako commented Jul 22, 2014

something like this nadako@21a20fd

@nadako
Copy link
Member Author

nadako commented Jul 22, 2014

or is it better to actually have some special a_status for that? i still don't really understand the whole open/closed/const anon status stuff

@Simn
Copy link
Member

Simn commented Jul 24, 2014

Maybe this helps:

typedef X = {
    x: Int
}

typedef Xy = {
    x: Int,
    y: String
}

class Main {
    static function main() { }

    static function opened(a) {
        // a is a monomorph here
        // field access turns it into an open anon
        a.x = 12;
        a.y = "foo";
        // note the +
        $type(a); // {+ y : String, x : Int }

        // unify with closed anon Xy
        var a2:Xy = a;
        // a is closed now
        $type(a); // { y : String, x : Int }

        // cannot add any more fields
        a.z = true;

        // can assign to less specific anon
        var a3:X = a;
    }

    static function const() {
        var a = { x: 12, y: "foo" };
        // a is const here, prints the same
        $type(a); // { y : String, x : Int }

        // unifies with closed, remains const
        var a2:Xy = a;

        // does not unify with less specific anon
        var a3:X = a;
    }
}

Open and closed status is quite straightforward: Open anons are only relevant when accessing fields on monomorphs. They allow additional fields to be added. Upon unification they are closed, which means they do not accept any more fields.

Const status is more peculiar and I can never remember why it exists in the first place, although it always makes sense when Nicolas explains it (again). It is relevant when working with structure declarations and as far as I can tell the distinguishing feature is that they do not unify with less specific anon types.

For your case I'd say the new status should be treated just ilke Closed.

@nadako
Copy link
Member Author

nadako commented Jul 28, 2014

Am I right thinking that extension info is only relevant for the Closed status? Then it would make sense to put that info in the Closed argument, but this is a breaking change (we only exposed "AnonStatus" in 3.1 tho).

So what should we do? Here's an implementation with an extra tanon field: nadako/haxe@21a20fd

@nadako
Copy link
Member Author

nadako commented Aug 5, 2014

any comments?:)

@ncannasse
Copy link
Member

Add a new case for a_status, that's the best solution

@nadako
Copy link
Member Author

nadako commented Aug 5, 2014

Okay, I've added a pull request #3228 adding new anon_status variant. Please review and merge!

nadako added a commit to nadako/haxe that referenced this issue Aug 5, 2014
@nadako nadako closed this as completed in 715f727 Aug 6, 2014
ncannasse added a commit that referenced this issue Aug 6, 2014
store structure extended types and expose it to the macro api with AnonStatus.AExtend (closes #3198)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform-macro Everything related to Haxe macros
Projects
None yet
Development

No branches or pull requests

4 participants