Std.hx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #if !(core_api || cross)
  23. #error "Please don't add haxe/std to your classpath, instead set HAXE_STD_PATH env var"
  24. #end
  25. /**
  26. The Std class provides standard methods for manipulating basic types.
  27. **/
  28. extern class Std {
  29. /**
  30. Tells if a value `v` is of the type `t`. Returns `false` if `v` or `t` are null.
  31. **/
  32. public static function is( v : Dynamic, t : Dynamic ) : Bool;
  33. /**
  34. Checks if object `value` is an instance of class `c`.
  35. Compiles only if the class specified by `c` can be assigned to the type
  36. of `value`.
  37. This method checks if a downcast is possible. That is, if the runtime
  38. type of `value` is assignable to the class specified by `c`, `value` is
  39. returned. Otherwise null is returned.
  40. This method is not guaranteed to work with interfaces or core types such
  41. as `String`, `Array` and `Date`.
  42. If `value` is null, the result is null. If `c` is null, the result is
  43. unspecified.
  44. **/
  45. public static function instance<T:{},S:T>( value : T, c : Class<S> ) : S;
  46. /**
  47. Converts any value to a String.
  48. If `s` is of `String`, `Int`, `Float` or `Bool`, its value is returned.
  49. If `s` is an instance of a class and that class or one of its parent classes has
  50. a `toString` method, that method is called. If no such method is present, the result
  51. is unspecified.
  52. If `s` is an enum constructor without argument, the constructor's name is returned. If
  53. arguments exists, the constructor's name followed by the String representations of
  54. the arguments is returned.
  55. If `s` is a structure, the field names along with their values are returned. The field order
  56. and the operator separating field names and values are unspecified.
  57. If s is null, "null" is returned.
  58. **/
  59. public static function string( s : Dynamic ) : String;
  60. /**
  61. Converts a `Float` to an `Int`, rounded towards 0.
  62. If `x` is outside of the signed Int32 range, or is `NaN`, `NEGATIVE_INFINITY` or `POSITIVE_INFINITY`, the result is unspecified.
  63. **/
  64. public static function int( x : Float ) : Int;
  65. /**
  66. Converts a `String` to an `Int`.
  67. Leading whitespaces are ignored.
  68. If `x` starts with 0x or 0X, hexadecimal notation is recognized where the following digits may
  69. contain 0-9 and A-F.
  70. Otherwise `x` is read as decimal number with 0-9 being allowed characters. `x` may also start with
  71. a - to denote a negative value.
  72. In decimal mode, parsing continues until an invalid character is detected, in which case the
  73. result up to that point is returned. For hexadecimal notation, the effect of invalid characters
  74. is unspecified.
  75. Leading 0s that are not part of the 0x/0X hexadecimal notation are ignored, which means octal
  76. notation is not supported.
  77. If the input cannot be recognized, the result is `null`.
  78. **/
  79. public static function parseInt( x : String ) : Null<Int>;
  80. /**
  81. Converts a `String` to a `Float`.
  82. The parsing rules for `parseInt` apply here as well, with the exception of invalid input
  83. resulting in a `NaN` value instead of null.
  84. Additionally, decimal notation may contain a single `.` to denote the start of the fractions.
  85. **/
  86. public static function parseFloat( x : String ) : Float;
  87. /**
  88. Return a random integer between 0 included and `x` excluded.
  89. If `x <= 1`, the result is always 0.
  90. **/
  91. public static function random( x : Int ) : Int;
  92. }