Main.hx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. class Main {
  2. static function main() {
  3. var comp = new AComponent([1, 2, 3]);
  4. trace(comp.doSomething());
  5. }
  6. }
  7. interface Component<T> {
  8. function doSomething():T;
  9. }
  10. @:forward
  11. @:multiType
  12. abstract AComponent<T>(Component<T>) {
  13. public function new(value:T);
  14. @:to public static inline function toInt(t:Component<Int>, value:Int):IntComponent {
  15. return new IntComponent(value);
  16. }
  17. @:to public static inline function toIntArray(t:Component<Array<Int>>, value:Array<Int>):ArrayComponent<Int> {
  18. return new ArrayComponent(value);
  19. }
  20. }
  21. @:generic
  22. @:remove
  23. class ArrayComponent<T> implements Component<Array<T>> {
  24. final value:Array<T>;
  25. public function new(value:Array<T>) {
  26. this.value = value;
  27. var x = [];
  28. for (i in 0...value.length) {
  29. var y = new AComponent(this.value[i]).doSomething();
  30. x.push(y);
  31. }
  32. }
  33. public function doSomething():Array<T> {
  34. return this.value;
  35. }
  36. }
  37. class IntComponent implements Component<Int> {
  38. final value:Int;
  39. public function new(value:Int) {
  40. this.value = value;
  41. }
  42. public function doSomething():Int {
  43. return value;
  44. }
  45. }