summaryrefslogtreecommitdiff
path: root/tests/examplefiles/example.hx
blob: 7584fc81e0e7345cc5e735db1ecf89ff93ce8e7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
 * This is not really a valid Haxe file, but just an demo...
 */

package;
package net.onthewings;

import net.onthewings.Test;
import net.onthewings.*;

using Lambda;
using net.onthewings.Test;

#if flash8
// Haxe code specific for flash player 8
#elseif flash
// Haxe code specific for flash platform (any version)
#elseif js
// Haxe code specific for javascript plaform
#elseif neko
// Haxe code specific for neko plaform
#else 
// do something else
    #error  // will display an error "Not implemented on this platform"
    #error "Custom error message" // will display an error "Custom error message"
#end

0; // Int
-134; // Int
0xFF00; // Int

123.0; // Float
.14179; // Float
13e50; // Float
-1e-99; // Float

"hello"; // String
"hello \"world\" !"; // String
'hello "world" !'; // String

true; // Bool
false; // Bool

null; // Unknown<0>

~/[a-z]+/i; // EReg : regular expression

var point = { "x" : 1, "y" : -5 };

{
    var x;
    var y = 3;
    var z : String;
    var w : String = "";
    var a, b : Bool, c : Int = 0;
}

//haxe3 pattern matching
switch(e.expr) {
	case EConst(CString(s)) if (StringTools.startsWith(s, "foo")):
		"1";
	case EConst(CString(s)) if (StringTools.startsWith(s, "bar")):
		"2";
	case EConst(CInt(i)) if (switch(Std.parseInt(i) * 2) { case 4: true; case _: false; }):
		"3";
	case EConst(_):
		"4";
	case _:
		"5";
}

switch [true, 1, "foo"] {
	case [true, 1, "foo"]: "0";
	case [true, 1, _]: "1";
	case _: "_";
}


class Test <T:Void->Void> {
	private function new():Void {
		inline function innerFun(a:Int, b:Int):Int {
			return readOnlyField = a + b;
		}
		
		_innerFun(1, 2.3);
	}
	
	static public var instance(get,null):Test;
	static function get_instance():Test {
		return instance != null ? instance : instance = new Test();
	}
}

@:native("Test") private class Test2 {}

extern class Ext {}

@:macro class M {
	@:macro static function test(e:Array<Expr>):ExprOf<String> {
		return macro "ok";
	}
}

enum Color {
    Red;
    Green;
    Blue;
    Grey( v : Int );
    Rgb( r : Int, g : Int, b : Int );
    Alpha( a : Int, col : Color );
}

class Colors {
    static function toInt( c : Color ) : Int {
        return switch( c ) {
            case Red: 0xFF0000;
            case Green: 0x00FF00;
            case Blue: 0x0000FF;
            case Grey(v): (v << 16) | (v << 8) | v;
            case Rgb(r,g,b): (r << 16) | (g << 8) | b;
            case Alpha(a,c): (a << 24) | (toInt(c) & 0xFFFFFF);
        }
    }
}

class EvtQueue<T : (Event, EventDispatcher)> {
    var evt : T;
}

typedef DS = Dynamic<String>;
typedef Pt = {
	var x:Float;
	var y:Float;
	@:optional var z:Float; /* optional z */
	function add(pt:Pt):Void;
}
typedef Pt2 = {
	x:Float,
	y:Float,
	?z:Float, //optional z
	add : Point -> Void,
}


//top-level class members
public function test();
private var attr(get, set) = 1;


//pre-proc number
public static inline function indexOf<T>(arr:Array<T>, v:T) : Int
{
	#if (haxe_ver >= 3.1) 
	return arr.indexOf(v);
	#else
		#if (flash || js)
		return untyped arr.indexOf(v);
		#else
		return std.Lambda.indexOf(arr, v);
		#end
	#end
}

//macro reification
var e = macro var $myVar = 0;
var e = macro ${v}.toLowerCase();
var e = macro o.$myField;
var e = macro { $myField : 0 };
var e = macro $i{varName}++;
var e = macro $v{myStr};
var args = [macro "sub", macro 3];
var e = macro "Hello".toLowerCase($a{args});
(macro $i{tmp}.addAtom($v{name}, $atom)).finalize(op.pos);

var c = macro class MyClass {
    public function new() { }
    public function $funcName() {
        trace($v{funcName} + " was called");
    }
}

var c = macro interface IClass {};

//macro class could have no name...
var def = macro class {
	private inline function new(loader) this = loader;
	private var loader(get,never) : $loaderType;
	inline private function get_loader() : $loaderType return this;
};

//ECheckType
var f = (123:Float);