MyAbstract.hx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. package unit;
  2. abstract MyAbstract(Int) {
  3. public inline function new(x) {
  4. this = x;
  5. }
  6. public inline function incr() {
  7. return ++this;
  8. }
  9. public inline function toInt() : Int {
  10. return this;
  11. }
  12. }
  13. #if !flash8
  14. abstract TemplateWrap(haxe.Template) {
  15. public inline function new(x) {
  16. this = new haxe.Template(x);
  17. }
  18. public inline function get()
  19. return this;
  20. @:from static inline public function fromString(s:String) {
  21. return new TemplateWrap(s);
  22. }
  23. @:to inline function toString() {
  24. return this.execute( { t: "really works!"});
  25. }
  26. }
  27. #end
  28. abstract Meter(Float) from Float to Float {
  29. public inline function new(f)
  30. this = f;
  31. public inline function get()
  32. return this;
  33. @:to public inline function toString()
  34. return this + "m";
  35. }
  36. abstract Kilometer(Float) from Float to Float {
  37. public inline function new(f)
  38. this = f;
  39. @:to public inline function toString()
  40. return this + "km";
  41. @:from static public inline function fromMeter(m:Meter)
  42. return new Kilometer(m.get() / 1000.);
  43. }
  44. class MyClassWithAbstractArgCtor {
  45. public var km:Kilometer;
  46. public function new(km:Kilometer) {
  47. this.km = km;
  48. }
  49. }
  50. abstract MyHash<V>(haxe.ds.StringMap<V>) {
  51. private inline function new() {
  52. this = new haxe.ds.StringMap<V>();
  53. }
  54. public inline function set(k:String, v:V)
  55. this.set(k, v);
  56. public inline function get(k:String)
  57. return this.get(k);
  58. public inline function toString()
  59. return this.toString();
  60. @:from static public function fromStringArray(arr:Array<String>):MyHash<String> {
  61. var hash = new MyHash();
  62. var i = 0;
  63. while (i < arr.length) {
  64. var k = arr[i++];
  65. var v = arr[i++];
  66. hash.set(k, v);
  67. }
  68. return hash;
  69. }
  70. @:from static public function fromArray<K>(arr:Array<K>) {
  71. var hash = new MyHash();
  72. var i = 0;
  73. while (i < arr.length) {
  74. var k = arr[i++];
  75. var v = arr[i++];
  76. hash.set(Std.string('_s$k'), v);
  77. }
  78. return hash;
  79. }
  80. }
  81. class AbstractBase<T> {
  82. public var value:T;
  83. public function new(value:T) {
  84. this.value = value;
  85. }
  86. }
  87. abstract AbstractZ<T>(AbstractBase<T>) from AbstractBase<T> {
  88. @:to public static function toFoo(a:AbstractBase<Int>):Int {
  89. return a.value;
  90. }
  91. @:to public static function toString(a:AbstractBase<String>):String {
  92. return a.value;
  93. }
  94. }
  95. class MyPoint3 {
  96. public var x:Float;
  97. public var y:Float;
  98. public var z:Float;
  99. public function new(x, y, z) {
  100. this.x = x;
  101. this.y = y;
  102. this.z = z;
  103. }
  104. }
  105. abstract MyVector(MyPoint3) from MyPoint3 to MyPoint3 {
  106. var x(get, set):Float;
  107. var y(get, set):Float;
  108. var z(get, set):Float;
  109. public function get_x() return this.x;
  110. public function get_y() return this.y;
  111. public function get_z() return this.z;
  112. public function set_x(x) return this.x = x;
  113. public function set_y(y) return this.y = y;
  114. public function set_z(z) return this.z = z;
  115. @:op(A + B) static public inline function add(lhs:MyVector, rhs:MyVector):MyVector {
  116. return new MyPoint3(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);
  117. }
  118. @:op(A *= B) static public inline function scalarAssign(lhs:MyVector, rhs:Float):MyVector {
  119. lhs.x *= rhs;
  120. lhs.y *= rhs;
  121. lhs.z *= rhs;
  122. return lhs;
  123. }
  124. @:op(A * B) static public inline function scalar(lhs:MyVector, rhs:Float):MyVector {
  125. return new MyPoint3(lhs.x * rhs, lhs.y * rhs, lhs.z * rhs);
  126. }
  127. @:op(-A) static public inline function invert(t:MyVector):MyVector {
  128. return new MyPoint3( -t.x, -t.y, -t.z);
  129. }
  130. public inline function get():MyPoint3
  131. return this;
  132. @:to public inline function toString():String
  133. return untyped '(${this.x},${this.y},${this.z})';
  134. }
  135. abstract MyInt(Int) from Int to Int {
  136. // MyInt + MyInt can be used as is, and returns a MyInt
  137. @:op(A + B) static public function add(lhs:MyInt, rhs:MyInt):MyInt;
  138. @:commutative @:op(A * B) static public function repeat(lhs:MyInt, rhs:String):String {
  139. var s:StringBuf = new StringBuf();
  140. for (i in 0...lhs)
  141. s.add(rhs);
  142. return s.toString();
  143. }
  144. @:op(A / B) static public function cut(lhs:String, rhs:MyInt) {
  145. return lhs.substr(0, rhs);
  146. }
  147. }
  148. abstract MyInt2(Int){
  149. public inline function new(v) {
  150. this = v;
  151. }
  152. public function get():Int {
  153. return this;
  154. }
  155. @:op(-x) public inline function invert():MyInt2 {
  156. return new MyInt2(-this);
  157. }
  158. @:op(++x) public inline function incr() {
  159. ++this;
  160. }
  161. }
  162. abstract MyString(String) from String to String {
  163. @:op(A + B) static public function add(lhs:MyString, rhs:MyString):MyString;
  164. @:op(A + B) static public function addInt(lhs:MyString, rhs:Int):MyString;
  165. @:op(A + B) static public function addBool(lhs:MyString, rhs:Bool):Bool;
  166. @:op(A - B) static public function sub(lhs:MyString, rhs:MyString):MyString;
  167. }
  168. class ClassWithHashCode {
  169. var i:Int;
  170. public function new(i) { this.i = i; }
  171. public function hashCode() return i;
  172. }
  173. class ClassWithoutHashCode {
  174. public var i:Int;
  175. public function new(i) { this.i = i; }
  176. }
  177. abstract MyReflect({}) from {} {
  178. @:arrayAccess public inline function arrayAccess(key:String):Dynamic {
  179. return Reflect.field(this, key);
  180. }
  181. @:arrayAccess public inline function arrayWrite<T>(key:String, value:T):T {
  182. Reflect.setField(this, key, value);
  183. return value;
  184. }
  185. }
  186. abstract MyAbstractClosure(String){
  187. public function new(value:String) {
  188. this = value;
  189. }
  190. public function test() {
  191. var fn = function(){
  192. return this;
  193. }
  194. return fn;
  195. }
  196. public inline function setVal(v) {
  197. this = v;
  198. }
  199. }
  200. abstract MyAbstractSetter(Dynamic) {
  201. public var value(get,set):String;
  202. public inline function new() {
  203. this = {};
  204. }
  205. inline function get_value() {
  206. return this.value;
  207. }
  208. inline function set_value(s:String) {
  209. this.value = s;
  210. return s;
  211. }
  212. }
  213. abstract MyAbstractCounter(Int) {
  214. public static var counter = 0;
  215. inline function new(v:Int) {
  216. this = v;
  217. counter++;
  218. }
  219. @:from inline static public function fromInt(v:Int) {
  220. return new MyAbstractCounter(v);
  221. }
  222. inline public function getValue():Int return this + 1;
  223. }
  224. abstract MyAbstractThatCallsAMember(Int) to Int {
  225. public function new(i) {
  226. this = i;
  227. bar();
  228. }
  229. inline function bar() this++;
  230. }
  231. abstract MyDebugString(String) to String {
  232. public inline function new(s:String) {
  233. this = s;
  234. }
  235. public inline function substr(i:Int, ?len:Null<Int>) {
  236. return this.substr(i);
  237. }
  238. }
  239. @:multiType abstract MySpecialString(String) {
  240. public function new(value:String);
  241. public inline function substr(i:Int, ?len:Int) {
  242. return len == null ? this.substr(i) : this.substr(i, len);
  243. }
  244. @:to static inline public function toNormal(t:String, value:String) {
  245. return new MyDebugString(value);
  246. }
  247. }