There are many processes you
can apply to values that are relevant to numerous data types. For example, the addition operator ( + ) can
be used with Float s, Int s and String s. With Float s and Int s, the addition operator adds the values
together, while using it with a pair of String s will concatenate them. This means that the following
example is perfectly legal and useful, if only slightly:
public static function main()
{
// Outputs: 4
trace( double( 2 ) );
(continued)
88
Part I: The Core Language
// Outputs: 5
trace( double( 2.5 ) );
// Outputs: haXehaXe
trace( double( ???haXe??? ) );
}
public static function double( num : Dynamic ) : Dynamic
{
return num + num;
}
When you use the Std.is() function, it should be possible to provide alternative functionality for types
where certain processes aren ??™ t quite suitable. For example, you could rewrite the previous example
like this:
public static function main()
{
// Outputs: 4
trace( double( 2 ) );
// Outputs: 5
trace( double( 2.5 ) );
// Outputs: haXe haXe
trace( double( ???haXe??? ) );
// Outputs: [ 1, 2, 3, 1, 2, 3 ]
trace( double( [ 1, 2, 3 ] ) );
// Outputs: null
trace( double( { param : ???value??? } ) );
}
public static function double( val : Dynamic ) : Dynamic
{
if ( Std.
Pages:
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207