EnumTools.hx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (C)2005-2017 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. package haxe;
  23. /**
  24. This class provides advanced methods on enums. It is ideally used with
  25. `using EnumTools` and then acts as an
  26. [extension](https://haxe.org/manual/lf-static-extension.html) to the
  27. `enum` types.
  28. If the first argument to any of the methods is null, the result is
  29. unspecified.
  30. **/
  31. extern class EnumTools {
  32. /**
  33. Returns the name of enum `e`, including its path.
  34. If `e` is inside a package, the package structure is returned dot-
  35. separated, with another dot separating the enum name:
  36. pack1.pack2.(...).packN.EnumName
  37. If `e` is a sub-type of a Haxe module, that module is not part of the
  38. package structure.
  39. If `e` has no package, the enum name is returned.
  40. If `e` is `null`, the result is unspecified.
  41. The enum name does not include any type parameters.
  42. **/
  43. static public inline function getName<T>(e:Enum<T>):String {
  44. return Type.getEnumName(e);
  45. }
  46. /**
  47. Creates an instance of enum `e` by calling its constructor `constr` with
  48. arguments `params`.
  49. If `e` or `constr` is `null`, or if enum `e` has no constructor named
  50. `constr`, or if the number of elements in `params` does not match the
  51. expected number of constructor arguments, or if any argument has an
  52. invalid type, the result is unspecified.
  53. **/
  54. static public inline function createByName<T>(e:Enum<T>, constr:String, ?params:Array<Dynamic>):T {
  55. return Type.createEnum(e, constr, params);
  56. }
  57. /**
  58. Creates an instance of enum `e` by calling its constructor number
  59. `index` with arguments `params`.
  60. The constructor indices are preserved from Haxe syntax, so the first
  61. declared is index 0, the next index 1 etc.
  62. If `e` or `index` is `null`, or if enum `e` has no constructor
  63. corresponding to index `index`, or if the number of elements in `params`
  64. does not match the expected number of constructor arguments, or if any
  65. argument has an invalid type, the result is unspecified.
  66. **/
  67. static public inline function createByIndex<T>(e:Enum<T>, index:Int, ?params:Array<Dynamic>):T {
  68. return Type.createEnumIndex(e, index, params);
  69. }
  70. /**
  71. Returns a list of all constructors of enum `e` that require no
  72. arguments.
  73. This may return the empty Array `[]` if all constructors of `e` require
  74. arguments.
  75. Otherwise an instance of `e` constructed through each of its non-
  76. argument constructors is returned, in the order of the constructor
  77. declaration.
  78. If `e` is `null`, the result is unspecified.
  79. **/
  80. static public inline function createAll<T>(e:Enum<T>):Array<T> {
  81. return Type.allEnums(e);
  82. }
  83. /**
  84. Returns a list of the names of all constructors of enum `e`.
  85. The order of the constructor names in the returned Array is preserved
  86. from the original syntax.
  87. If `c` is `null`, the result is unspecified.
  88. **/
  89. static public inline function getConstructors<T>(e:Enum<T>):Array<String> {
  90. return Type.getEnumConstructs(e);
  91. }
  92. }
  93. /**
  94. This class provides advanced methods on enum values. It is ideally used with
  95. `using EnumValueTools` and then acts as an
  96. [extension](https://haxe.org/manual/lf-static-extension.html) to the
  97. `EnumValue` types.
  98. If the first argument to any of the methods is null, the result is
  99. unspecified.
  100. **/
  101. extern class EnumValueTools {
  102. /**
  103. Recursively compares two enum instances `a` and `b` by value.
  104. Unlike `a == b`, this function performs a deep equality check on the
  105. arguments of the constructors (if there are any).
  106. If `a` or `b` are `null`, the result is unspecified.
  107. **/
  108. static public inline function equals<T:EnumValue>(a:T, b:T):Bool {
  109. return Type.enumEq(a, b);
  110. }
  111. /**
  112. Returns the constructor name of enum instance `e`.
  113. The result String does not contain any constructor arguments.
  114. If `e` is `null`, the result is unspecified.
  115. **/
  116. static public inline function getName(e:EnumValue):String {
  117. return Type.enumConstructor(e);
  118. }
  119. /**
  120. Returns a list of the constructor arguments of enum instance `e`.
  121. If `e` has no arguments, the result is `[]`.
  122. Otherwise the result are the values that were used as arguments to `e`,
  123. in the order of their declaration.
  124. If `e` is `null`, the result is unspecified.
  125. **/
  126. static public inline function getParameters(e:EnumValue):Array<Dynamic> {
  127. return Type.enumParameters(e);
  128. }
  129. /**
  130. Returns the index of enum instance `e`.
  131. This corresponds to the original syntactic position of `e`. The index of
  132. the first declared constructor is 0, the next one is 1 etc.
  133. If `e` is `null`, the result is unspecified.
  134. **/
  135. static public inline function getIndex(e:EnumValue):Int {
  136. return Type.enumIndex(e);
  137. }
  138. /**
  139. Matches enum instance `e` against pattern `pattern`, returning `true` if
  140. matching succeeded and `false` otherwise.
  141. Example usage:
  142. ```haxe
  143. if (e.match(pattern)) {
  144. // codeIfTrue
  145. } else {
  146. // codeIfFalse
  147. }
  148. ```
  149. This is equivalent to the following code:
  150. ```haxe
  151. switch (e) {
  152. case pattern:
  153. // codeIfTrue
  154. case _:
  155. // codeIfFalse
  156. }
  157. ```
  158. This method is implemented in the compiler. This definition exists only
  159. for documentation.
  160. **/
  161. static public function match(e:EnumValue, pattern:Dynamic):Bool {
  162. return false;
  163. }
  164. }