Skip to content

Instantly share code, notes, and snippets.

@siratama
Created December 10, 2014 03:22
Show Gist options
  • Save siratama/4f46732f912ec031d8d1 to your computer and use it in GitHub Desktop.
Save siratama/4f46732f912ec031d8d1 to your computer and use it in GitHub Desktop.
Haxe abstract class decision and creation by Type.createInstance.
package com.dango_itimi.utils;
using com.dango_itimi.utils.TypeUtil;
class TypeUtil
{
public static function isAbstract(cls:Class<Dynamic>):Bool
{
var array = Type.getClassFields(cls);
for (string in array)
{
if(string == "_new") return true;
}
return false;
}
//cls contains abstract class
public static function createInstance<T>(cls:Class<T>, args:Array<Dynamic>):T
{
return !cls.isAbstract() ?
Type.createInstance(cast cls, args):
Type.createInstance(cast cls, args)._new();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment