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

Reflect.field issues #4107

Closed
ubald opened this issue Mar 31, 2015 · 6 comments
Closed

Reflect.field issues #4107

ubald opened this issue Mar 31, 2015 · 6 comments
Assignees
Labels
platform-cpp Everything related to CPP / C++
Milestone

Comments

@ubald
Copy link

ubald commented Mar 31, 2015

The following code:

package ;
class Main {
    static public function main( ) {
        trace(Reflect.field(A,'STATIC_FIELD'));
        trace(Reflect.field(new A(),'STATIC_FIELD'));
        trace(Reflect.field(B,'STATIC_FIELD'));
        trace(Reflect.field(new B(),'STATIC_FIELD'));
        trace(Reflect.field(C,'STATIC_FIELD'));
        trace(Reflect.field(new C(),'STATIC_FIELD'));
    }
}

class A {
    static public var STATIC_FIELD : String = "A: hello world";
    public function new() {}
}

class B extends A {
    public function new() { super(); }
}

class C extends B {
    static public var STATIC_FIELD : String = "C: hello world";
    public function new() { super(); }
}

Running with Haxe 3.1.3, mac cpp target, traces the following:

Main.hx:4: A: hello world
Main.hx:5: A: hello world
Main.hx:6: A: hello world
Main.hx:7: A: hello world
Main.hx:8: C: hello world
Main.hx:9: C: hello world

While running with Haxe 3.2 rc2, mac cpp target, it traces the following:

Main.hx:4: A: hello world
Main.hx:5: null
Main.hx:6: null
Main.hx:7: null
Main.hx:8: C: hello world
Main.hx:9: null
@Simn
Copy link
Member

Simn commented Mar 31, 2015

This is a bugfix, static fields should not be listed as part of the instance fields. I have added a note in the breaking change overview: https://github.com/HaxeFoundation/haxe/wiki/Breaking-changes-in-Haxe-3.2.0

@Simn Simn closed this as completed Mar 31, 2015
@ncannasse
Copy link
Member

This is a fix in Haxe 3.2, CPP is now correctly aligned with other targets behaviors : Reflect.field on instance will not be able to access statics, you need to use Reflect.field(Type.getClass(new A()),"STATIC_FIELD") for that.

@ubald
Copy link
Author

ubald commented Mar 31, 2015

I was more relying on the fact that the static fields were kind of inherited using this method ;)

@ubald
Copy link
Author

ubald commented Apr 1, 2015

I ended up making a utility class to handle my needs

package com.ubald.utils;
class ReflectionUtil {
    static public function staticClassField( obj : Class<Dynamic>, field : String ) : Dynamic {
        var type : Class<Dynamic> = obj;
        var value : Dynamic = null;
        while( type != null ) {
            value = Reflect.field( type, field );
            if ( value != null ) {
                break;
            }
            type = Type.getSuperClass( type );
        }
        return value;
    }
    static public function staticInstanceField( obj : Dynamic, field : String ) : Dynamic {
        return staticClassField( Type.getClass( obj ), field );
    }
}

It works for me but while coding it I found out that Reflect.hasField( ChildClass, 'SOME_FIELD' ); returns true if the static field is on a parent class, but Reflect.field( ChildClass, 'SOME_FIELD'); returns null. They should at least have the same behavior.

@Simn Simn reopened this Apr 1, 2015
@Simn Simn added the platform-cpp Everything related to CPP / C++ label Apr 1, 2015
@Simn Simn added this to the 3.2 milestone Apr 1, 2015
@Simn
Copy link
Member

Simn commented Apr 1, 2015

@hughsando: Could you check that?

@hughsando
Copy link
Member

This is now fixed on hxcpp to match neko. @Simn - Not sure if this is the "official" behaviour, and whether you want to add a test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform-cpp Everything related to CPP / C++
Projects
None yet
Development

No branches or pull requests

4 participants