Type.hx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. /**
  23. The Haxe Reflection API allows retrieval of type information at runtime.
  24. This class complements the more lightweight Reflect class, with a focus on
  25. class and enum instances.
  26. @see https://haxe.org/manual/types.html
  27. @see https://haxe.org/manual/std-reflection.html
  28. **/
  29. extern class Type {
  30. /**
  31. Returns the class of `o`, if `o` is a class instance.
  32. If `o` is null or of a different type, null is returned.
  33. In general, type parameter information cannot be obtained at runtime.
  34. **/
  35. static function getClass<T>(o:T):Class<T>;
  36. /**
  37. Returns the enum of enum instance `o`.
  38. An enum instance is the result of using an enum constructor. Given an
  39. `enum Color { Red; }`, `getEnum(Red)` returns `Enum<Color>`.
  40. If `o` is null, null is returned.
  41. In general, type parameter information cannot be obtained at runtime.
  42. **/
  43. static function getEnum(o:EnumValue):Enum<Dynamic>;
  44. /**
  45. Returns the super-class of class `c`.
  46. If `c` has no super class, null is returned.
  47. If `c` is null, the result is unspecified.
  48. In general, type parameter information cannot be obtained at runtime.
  49. **/
  50. static function getSuperClass(c:Class<Dynamic>):Class<Dynamic>;
  51. /**
  52. Returns the name of class `c`, including its path.
  53. If `c` is inside a package, the package structure is returned dot-
  54. separated, with another dot separating the class name:
  55. `pack1.pack2.(...).packN.ClassName`
  56. If `c` is a sub-type of a Haxe module, that module is not part of the
  57. package structure.
  58. If `c` has no package, the class name is returned.
  59. If `c` is null, the result is unspecified.
  60. The class name does not include any type parameters.
  61. **/
  62. static function getClassName(c:Class<Dynamic>):String;
  63. /**
  64. Returns the name of enum `e`, including its path.
  65. If `e` is inside a package, the package structure is returned dot-
  66. separated, with another dot separating the enum name:
  67. `pack1.pack2.(...).packN.EnumName`
  68. If `e` is a sub-type of a Haxe module, that module is not part of the
  69. package structure.
  70. If `e` has no package, the enum name is returned.
  71. If `e` is null, the result is unspecified.
  72. The enum name does not include any type parameters.
  73. **/
  74. static function getEnumName(e:Enum<Dynamic>):String;
  75. /**
  76. Resolves a class by name.
  77. If `name` is the path of an existing class, that class is returned.
  78. Otherwise null is returned.
  79. If `name` is null or the path to a different type, the result is
  80. unspecified.
  81. The class name must not include any type parameters.
  82. **/
  83. static function resolveClass(name:String):Class<Dynamic>;
  84. /**
  85. Resolves an enum by name.
  86. If `name` is the path of an existing enum, that enum is returned.
  87. Otherwise null is returned.
  88. If `name` is null the result is unspecified.
  89. If `name` is the path to a different type, null is returned.
  90. The enum name must not include any type parameters.
  91. **/
  92. static function resolveEnum(name:String):Enum<Dynamic>;
  93. /**
  94. Creates an instance of class `cl`, using `args` as arguments to the
  95. class constructor.
  96. This function guarantees that the class constructor is called.
  97. Default values of constructors arguments are not guaranteed to be
  98. taken into account.
  99. If `cl` or `args` are null, or if the number of elements in `args` does
  100. not match the expected number of constructor arguments, or if any
  101. argument has an invalid type, or if `cl` has no own constructor, the
  102. result is unspecified.
  103. In particular, default values of constructor arguments are not
  104. guaranteed to be taken into account.
  105. **/
  106. static function createInstance<T>(cl:Class<T>, args:Array<Dynamic>):T;
  107. /**
  108. Creates an instance of class `cl`.
  109. This function guarantees that the class constructor is not called.
  110. If `cl` is null, the result is unspecified.
  111. **/
  112. static function createEmptyInstance<T>(cl:Class<T>):T;
  113. /**
  114. Creates an instance of enum `e` by calling its constructor `constr` with
  115. arguments `params`.
  116. If `e` or `constr` is null, or if enum `e` has no constructor named
  117. `constr`, or if the number of elements in `params` does not match the
  118. expected number of constructor arguments, or if any argument has an
  119. invalid type, the result is unspecified.
  120. **/
  121. static function createEnum<T>(e:Enum<T>, constr:String, ?params:Array<Dynamic>):T;
  122. /**
  123. Creates an instance of enum `e` by calling its constructor number
  124. `index` with arguments `params`.
  125. The constructor indices are preserved from Haxe syntax, so the first
  126. declared is index 0, the next index 1 etc.
  127. If `e` or `constr` is null, or if enum `e` has no constructor named
  128. `constr`, or if the number of elements in `params` does not match the
  129. expected number of constructor arguments, or if any argument has an
  130. invalid type, the result is unspecified.
  131. **/
  132. static function createEnumIndex<T>(e:Enum<T>, index:Int, ?params:Array<Dynamic>):T;
  133. /**
  134. Returns a list of the instance fields of class `c`, including
  135. inherited fields.
  136. This only includes fields which are known at compile-time. In
  137. particular, using `getInstanceFields(getClass(obj))` will not include
  138. any fields which were added to `obj` at runtime.
  139. The order of the fields in the returned Array is unspecified.
  140. If `c` is null, the result is unspecified.
  141. **/
  142. static function getInstanceFields(c:Class<Dynamic>):Array<String>;
  143. /**
  144. Returns a list of static fields of class `c`.
  145. This does not include static fields of parent classes.
  146. The order of the fields in the returned Array is unspecified.
  147. If `c` is null, the result is unspecified.
  148. **/
  149. static function getClassFields(c:Class<Dynamic>):Array<String>;
  150. /**
  151. Returns a list of the names of all constructors of enum `e`.
  152. The order of the constructor names in the returned Array is preserved
  153. from the original syntax.
  154. If `e` is null, the result is unspecified.
  155. **/
  156. static function getEnumConstructs(e:Enum<Dynamic>):Array<String>;
  157. /**
  158. Returns the runtime type of value `v`.
  159. The result corresponds to the type `v` has at runtime, which may vary
  160. per platform. Assumptions regarding this should be minimized to avoid
  161. surprises.
  162. **/
  163. static function typeof(v:Dynamic):ValueType;
  164. /**
  165. Recursively compares two enum instances `a` and `b` by value.
  166. Unlike `a == b`, this function performs a deep equality check on the
  167. arguments of the constructors, if exists.
  168. If `a` or `b` are null, the result is unspecified.
  169. **/
  170. static function enumEq<T:EnumValue>(a:T, b:T):Bool;
  171. /**
  172. Returns the constructor name of enum instance `e`.
  173. The result String does not contain any constructor arguments.
  174. If `e` is null, the result is unspecified.
  175. **/
  176. static function enumConstructor(e:EnumValue):String;
  177. /**
  178. Returns a list of the constructor arguments of enum instance `e`.
  179. If `e` has no arguments, the result is [].
  180. Otherwise the result are the values that were used as arguments to `e`,
  181. in the order of their declaration.
  182. If `e` is null, the result is unspecified.
  183. **/
  184. static function enumParameters(e:EnumValue):Array<Dynamic>;
  185. /**
  186. Returns the index of enum instance `e`.
  187. This corresponds to the original syntactic position of `e`. The index of
  188. the first declared constructor is 0, the next one is 1 etc.
  189. If `e` is null, the result is unspecified.
  190. **/
  191. static function enumIndex(e:EnumValue):Int;
  192. /**
  193. Returns a list of all constructors of enum `e` that require no
  194. arguments.
  195. This may return the empty Array `[]` if all constructors of `e` require
  196. arguments.
  197. Otherwise an instance of `e` constructed through each of its non-
  198. argument constructors is returned, in the order of the constructor
  199. declaration.
  200. If `e` is null, the result is unspecified.
  201. **/
  202. static function allEnums<T>(e:Enum<T>):Array<T>;
  203. }
  204. /**
  205. The different possible runtime types of a value.
  206. **/
  207. enum ValueType {
  208. TNull;
  209. TInt;
  210. TFloat;
  211. TBool;
  212. TObject;
  213. TFunction;
  214. TClass(c:Class<Dynamic>);
  215. TEnum(e:Enum<Dynamic>);
  216. TUnknown;
  217. }