11package yutautil ;
22
3+ /**
4+ * OneOrMore is an alias for OneOrMany<T> where T is the type of the items.
5+ * It allows a variable to take either a single item of type T or an array of items of type T.
6+ * This is useful for functions that can accept either a single item or multiple items of the same type.
7+ */
38typedef OneOrMore <T > = OneOrMany <T >;
49
10+ /**
11+ * FlexibleNum is an abstract type that can represent either an Int or a Float.
12+ * It provides methods to convert between these types and to create instances from them.
13+ * This is useful for cases where you want to handle both Int and Float values uniformly.
14+ */
515abstract FlexibleNum (Float ) from Int from Float to Float {
616 public inline function new (value : Float ) {
717 this = value ;
@@ -28,6 +38,137 @@ abstract FlexibleNum(Float) from Int from Float to Float {
2838 }
2939}
3040
41+ /**
42+ * Suggestion is a wrapper which allows for suggesting a type for a Dynamic value.
43+ * It is used to provide type hints for Dynamic values, allowing them to be treated as a specific type.
44+ */
45+ abstract Suggestion <T > (Dynamic ) from Dynamic to Dynamic {
46+ public function new (value : Dynamic ) {
47+ this = value ;
48+ }
49+ }
50+
51+ private enum BasicTypesValue {
52+ TInt (v : Int );
53+ TFloat (v : Float );
54+ TString (v : String );
55+ TBool (v : Bool );
56+ }
57+
58+ /**
59+ * BasicTypes is an abstract type that can hold a value of Int, Float, String, or Bool.
60+ * It provides methods to convert between these types and to create instances from them.
61+ */
62+ abstract BasicTypes (BasicTypesValue ) {
63+ public function new (value : Dynamic ) {
64+ if (value == null ) {
65+ throw ' BasicTypes cannot be constructed with null value' ;
66+ }
67+ if (Std .isOfType (value , Int )) {
68+ this = TInt (value );
69+ } else if (Std .isOfType (value , Float )) {
70+ this = TFloat (value );
71+ } else if (Std .isOfType (value , String )) {
72+ this = TString (value );
73+ } else if (Std .isOfType (value , Bool )) {
74+ this = TBool (value );
75+ } else {
76+ throw ' BasicTypes can only be constructed with Int, Float, String, or Bool' ;
77+ }
78+ }
79+
80+ @:from
81+ public static inline function fromInt (value : Int ): BasicTypes {
82+ return new BasicTypes (value );
83+ }
84+
85+ @:from
86+ public static inline function fromFloat (value : Float ): BasicTypes {
87+ return new BasicTypes (value );
88+ }
89+
90+ @:from
91+ public static inline function fromString (value : String ): BasicTypes {
92+ return new BasicTypes (value );
93+ }
94+
95+ @:from
96+ public static inline function fromBool (value : Bool ): BasicTypes {
97+ return new BasicTypes (value );
98+ }
99+
100+ @:to
101+ public inline function toInt (): Int {
102+ return switch (this ) {
103+ case TInt (v ): v ;
104+ case TFloat (v ): Std .int (v );
105+ case TString (v ): Std .parseInt (v );
106+ case TBool (v ): v ? 1 : 0 ;
107+ }
108+ }
109+ @:to
110+ public inline function toFloat (): Float {
111+ return switch (this ) {
112+ case TInt (v ): v ;
113+ case TFloat (v ): v ;
114+ case TString (v ): Std .parseFloat (v );
115+ case TBool (v ): v ? 1.0 : 0.0 ;
116+ }
117+ }
118+ @:to
119+ public inline function toString (): String {
120+ return switch (this ) {
121+ case TInt (v ): Std .string (v );
122+ case TFloat (v ): Std .string (v );
123+ case TString (v ): v ;
124+ case TBool (v ): Std .string (v );
125+ }
126+ }
127+ @:to
128+ public inline function toBool (): Bool {
129+ return switch (this ) {
130+ case TInt (v ): v != 0 ;
131+ case TFloat (v ): v != 0.0 ;
132+ case TString (v ): v != " " && v != " 0" && v != " false" ;
133+ case TBool (v ): v ;
134+ }
135+ }
136+ }
137+
138+ /* * SuggestionArray is an alias for SuggestionArray<T> where T is the type of
139+ * the suggestions. It allows for a more concise syntax when working with
140+ * arrays of suggestions.
141+ */
142+ typedef ArraySuggestion <T > = SuggestionArray <T >;
143+
144+ /*
145+ * SuggestionArray is an abstract type that wraps an Array<Suggestion<T>>.
146+ * It provides methods to convert from/to Array<Suggestion<T>> and to handle
147+ * single or multiple suggestions.
148+ */
149+ abstract SuggestionArray <T >(Array <Suggestion <T >>) to Array <Suggestion <T >> {
150+ public function new (value : Array <Suggestion <T >>) {
151+ this = value ;
152+ }
153+
154+ @:from
155+ public static inline function fromArray <T >(arr : Array <T >): SuggestionArray <T > {
156+ return cast arr ;
157+ }
158+
159+ @:to
160+ public inline function toArray (): Array <Suggestion <T >> {
161+ return this ;
162+ }
163+ }
164+
165+ /**
166+ * OneOrMany is an abstract type that can represent either a single value or an array of values.
167+ * It provides methods to convert between a single value and an array, and to check if it contains
168+ * a single value or multiple values.
169+ * It is useful for cases where you want to handle both single and multiple items uniformly, especially in functions
170+ * that can accept either a single item or an array of items.
171+ */
31172abstract OneOrMany <T >(Array <T >) to Array <T > {
32173 // Construct from a single value
33174 public function new (value : T ) {
@@ -42,13 +183,13 @@ abstract OneOrMany<T>(Array<T>) to Array<T> {
42183
43184 // Construct from an array
44185 @:from
45- public static inline function fromArray <T >(arr : Array <T >): OneOrMany <T > {
186+ public static inline function asMore <T >(arr : Array <T >): OneOrMany <T > {
46187 return cast arr ;
47188 }
48189
49190 // Construct from a single value (implicit)
50191 @:from
51- public static inline function fromSingle <T >(value : T ): OneOrMany <T > {
192+ public static inline function actAsArray <T >(value : T ): OneOrMany <T > {
52193 return new OneOrMany (value );
53194 }
54195
0 commit comments