Skip to content

Instantly share code, notes, and snippets.

@skial
Created November 17, 2011 00:03
Show Gist options
  • Save skial/1371946 to your computer and use it in GitHub Desktop.
Save skial/1371946 to your computer and use it in GitHub Desktop.
Weird behaviour with static methods...

With the externs classes A, B and C, used as A.createB().createC().hello I get Cannot access static field createC from a class instance

If I use real classes RealA, RealB and RealC, used as RealA.createRealB().createRealC().hello I get the same error as before. But if I remove the return type from each createReal method, the code compiles and works as expected.

In the titanium desktop externs, quite afew methods use the layout shown above. API.getApplication().getArgument() causes the same error above.

I think I'm doing something stupid, and I'm not seeing it.

:|

package com.bainn;
extern class A {
public static function createB():B;
}
package com.bainn;
extern class B {
public static function createC():C;
}
package com.bainn;
extern class C {
public static var hello:String;
}
package ;
import com.bainn.A;
import com.bainn.B;
import com.bainn.C;
import com.bainn.RealA;
import com.bainn.RealB;
import com.bainn.RealC;
import js.Lib;
class Main {
static function main() {
//A.createB().createC().hello; // src/Main.hx:14: characters 2-21 : Cannot access static field createC from a class instance
RealA.createRealB().createRealC().hello;
}
}
package com.bainn;
class RealA {
public static function createRealB():RealB {
return RealB;
}
public function new() {
}
}
package com.bainn;
class RealB {
public static function createRealC():RealC {
return RealC;
}
public function new() {
}
}
package com.bainn;
class RealC {
public static var hello:String = 'hello';
public function new() {
}
}
@ncannasse
Copy link

You're mistaking the "type" RealC which means "an instance of the class Realc" with the "value-class" RealC which is an object which contain both static methods and properties.

@skial
Copy link
Author

skial commented Nov 18, 2011

Thanks Nicolas, yesterday morning it suddenly became clear what I was doing wrong :)

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