I'm having a hard time figuring out how to use Generics and casting instances to that type. Below is a test class with all of the errors commented next to the lines.
typedef TestClassSprite = TestClass<Sprite>;
class TestCast extends State {
override public function create():Void {
super.create();
var foo:Dynamic = new TestClass<Sprite>("yup");
// EXPECTING - true
if (Std.is(foo, TestClass))// ERROR - Invalid number of type parameters for com.geokureli.testbed.misc.TestClass
trace(cast(foo, TestClass).name);// ERROR - Invalid number of type parameters for com.geokureli.testbed.misc.TestClass
// EXPECTING - true
if (Std.is(foo, TestClass<DisplayObject>))// ERROR - Unexpected )
trace(cast(foo, TestClass<DisplayObject>).name);// ERROR - Cast type parameters must be Dynamic
// EXPECTING - true
if (Std.is(foo, TestClass<Sprite>))// ERROR - Unexpected )
trace(cast(foo, TestClass<Sprite>).name);// ERROR - Cast type parameters must be Dynamic
// EXPECTING - true
if (Std.is(foo, TestClassSprite))
trace(cast(foo, TestClassSprite).name);// ERROR - Cast type parameters must be Dynamic
// EXPECTING - false
if (Std.is(foo, TestClass<MovieClip>))// ERROR - Unexpected )
trace(cast(foo, TestClass<MovieClip>).name);// ERROR - Cast type parameters must be Dynamic
// EXPECTING - true
if (Std.is(foo, TestClass<Dynamic>))// ERROR - Unexpected )
trace(cast(foo, TestClass<Dynamic>).name);
}
}
class TestClass<T:DisplayObject> {
public var name:String;
public function new(name:String) { this.name = name; }
}
how do I use these? how do I upcast them?
EDIT: OMG When did newgrounds get an edit button!!!
P.S. sorry if this is the wrong forum, things have changed a bit around here.