2013-08-19 22:55:01 +04:00
|
|
|
---
|
|
|
|
language: haxe
|
|
|
|
filename: LearnHaxe3.hx
|
|
|
|
contributors:
|
|
|
|
- ["Justin Donaldson", "https://github.com/jdonaldson/"]
|
2015-03-24 01:21:28 +03:00
|
|
|
- ["Dan Korostelev", "https://github.com/nadako/"]
|
2013-08-19 22:55:01 +04:00
|
|
|
---
|
|
|
|
|
2019-09-30 05:42:01 +03:00
|
|
|
[Haxe](https://haxe.org/) is a general-purpose language that provides platform support for C++, C#,
|
|
|
|
Swf/ActionScript, JavaScript, Java, PHP, Python, Lua, HashLink, and Neko bytecode
|
2019-02-21 00:51:55 +03:00
|
|
|
(the latter two being also written by the Haxe author). Note that this guide is for
|
2019-02-21 00:46:04 +03:00
|
|
|
Haxe version 3. Some of the guide may be applicable to older versions, but it is
|
|
|
|
recommended to use other references.
|
2013-08-19 22:55:01 +04:00
|
|
|
|
2024-04-08 17:07:03 +03:00
|
|
|
```haxe
|
2013-08-20 08:54:38 +04:00
|
|
|
/*
|
|
|
|
Welcome to Learn Haxe 3 in 15 minutes. http://www.haxe.org
|
|
|
|
This is an executable tutorial. You can compile and run it using the haxe
|
|
|
|
compiler, while in the same directory as LearnHaxe.hx:
|
2019-02-21 01:20:29 +03:00
|
|
|
|
|
|
|
$ haxe -main LearnHaxe3 --interp
|
2013-08-19 22:55:01 +04:00
|
|
|
|
2013-08-23 03:38:07 +04:00
|
|
|
Look for the slash-star marks surrounding these paragraphs. We are inside
|
|
|
|
a "Multiline comment". We can leave some notes here that will get ignored
|
|
|
|
by the compiler.
|
|
|
|
|
|
|
|
Multiline comments are also used to generate javadoc-style documentation for
|
|
|
|
haxedoc. They will be used for haxedoc if they immediately precede a class,
|
|
|
|
class function, or class variable.
|
2013-08-20 08:54:38 +04:00
|
|
|
*/
|
2013-08-19 22:55:01 +04:00
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// Double slashes like this will give a single-line comment.
|
2013-08-23 03:38:07 +04:00
|
|
|
|
2013-08-20 08:54:38 +04:00
|
|
|
/*
|
2013-08-23 03:38:07 +04:00
|
|
|
This is your first actual haxe code coming up, it's declaring an empty
|
2019-02-21 00:46:04 +03:00
|
|
|
package. A package isn't necessary, but it's useful if you want to create
|
|
|
|
a namespace for your code (e.g. org.yourapp.ClassName).
|
2015-03-24 01:21:28 +03:00
|
|
|
|
2015-03-24 12:37:49 +03:00
|
|
|
Omitting package declaration is the same as declaring an empty package.
|
2013-08-20 08:54:38 +04:00
|
|
|
*/
|
2013-08-19 22:55:01 +04:00
|
|
|
package; // empty package, no namespace.
|
|
|
|
|
2013-08-22 09:07:32 +04:00
|
|
|
/*
|
2015-03-24 01:21:28 +03:00
|
|
|
Packages are directories that contain modules. Each module is a .hx file
|
|
|
|
that contains types defined in a package. Package names (e.g. org.yourapp)
|
|
|
|
must be lower case while module names are capitalized. A module contain one
|
|
|
|
or more types whose names are also capitalized.
|
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
E.g, the class "org.yourapp.Foo" should have the folder structure
|
|
|
|
org/module/Foo.hx, as accessible from the compiler's working directory or
|
|
|
|
class path.
|
2013-08-23 03:38:07 +04:00
|
|
|
|
|
|
|
If you import code from other files, it must be declared before the rest of
|
|
|
|
the code. Haxe provides a lot of common default classes to get you started:
|
2013-08-22 09:07:32 +04:00
|
|
|
*/
|
2013-08-19 22:55:01 +04:00
|
|
|
import haxe.ds.ArraySort;
|
|
|
|
|
|
|
|
// you can import many classes/modules at once with "*"
|
|
|
|
import haxe.ds.*;
|
|
|
|
|
2015-03-24 01:21:28 +03:00
|
|
|
// you can import static fields
|
|
|
|
import Lambda.array;
|
|
|
|
|
|
|
|
// you can also use "*" to import all static fields
|
|
|
|
import Math.*;
|
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// You can also import classes in a special way, enabling them to extend the
|
|
|
|
// functionality of other classes like a "mixin". More on 'using' later.
|
2013-08-19 22:55:01 +04:00
|
|
|
using StringTools;
|
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// Typedefs are like variables... for types. They must be declared before any
|
|
|
|
// code. More on this later.
|
2013-08-22 09:07:32 +04:00
|
|
|
typedef FooString = String;
|
2013-08-19 22:55:01 +04:00
|
|
|
|
2013-08-23 03:38:07 +04:00
|
|
|
// Typedefs can also reference "structural" types, more on that later as well.
|
2013-08-22 09:07:32 +04:00
|
|
|
typedef FooObject = { foo: String };
|
2013-08-19 22:55:01 +04:00
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// Here's the class definition. It's the main class for the file, since it has
|
|
|
|
// the same name (LearnHaxe3).
|
|
|
|
class LearnHaxe3 {
|
2013-08-19 22:55:01 +04:00
|
|
|
/*
|
|
|
|
If you want certain code to run automatically, you need to put it in
|
|
|
|
a static main function, and specify the class in the compiler arguments.
|
|
|
|
In this case, we've specified the "LearnHaxe3" class in the compiler
|
|
|
|
arguments above.
|
|
|
|
*/
|
2019-02-21 00:46:04 +03:00
|
|
|
static function main() {
|
2013-08-19 22:55:01 +04:00
|
|
|
/*
|
|
|
|
Trace is the default method of printing haxe expressions to the
|
|
|
|
screen. Different targets will have different methods of
|
|
|
|
accomplishing this. E.g., java, c++, c#, etc. will print to std
|
|
|
|
out. Javascript will print to console.log, and flash will print to
|
|
|
|
an embedded TextField. All traces come with a default newline.
|
|
|
|
Finally, It's possible to prevent traces from showing by using the
|
|
|
|
"--no-traces" argument on the compiler.
|
|
|
|
*/
|
|
|
|
trace("Hello World, with trace()!");
|
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// Trace can handle any type of value or object. It will try to print
|
|
|
|
// a representation of the expression as best it can. You can also
|
|
|
|
// concatenate strings with the "+" operator:
|
2019-02-21 01:20:29 +03:00
|
|
|
trace("Integer: " + 10 + " Float: " + 3.14 + " Boolean: " + true);
|
2013-08-19 22:55:01 +04:00
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// In Haxe, it's required to separate expressions in the same block with
|
|
|
|
// semicolons. But, you can put two expressions on one line:
|
2013-08-19 22:55:01 +04:00
|
|
|
trace('two expressions..'); trace('one line');
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Types & Variables
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
trace("***Types & Variables***");
|
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// You can save values and references to data structures using the
|
|
|
|
// "var" keyword:
|
2013-08-19 22:55:01 +04:00
|
|
|
var an_integer:Int = 1;
|
|
|
|
trace(an_integer + " is the value for an_integer");
|
|
|
|
|
|
|
|
/*
|
|
|
|
Haxe is statically typed, so "an_integer" is declared to have an
|
|
|
|
"Int" type, and the rest of the expression assigns the value "1" to
|
|
|
|
it. It's not necessary to declare the type in many cases. Here,
|
|
|
|
the haxe compiler is inferring that the type of another_integer
|
|
|
|
should be "Int".
|
|
|
|
*/
|
|
|
|
var another_integer = 2;
|
|
|
|
trace(another_integer + " is the value for another_integer");
|
|
|
|
|
|
|
|
// The $type() method prints the type that the compiler assigns:
|
|
|
|
$type(another_integer);
|
|
|
|
|
|
|
|
// You can also represent integers with hexadecimal:
|
|
|
|
var hex_integer = 0xffffff;
|
|
|
|
|
|
|
|
/*
|
2013-08-20 08:54:38 +04:00
|
|
|
Haxe uses platform precision for Int and Float sizes. It also
|
|
|
|
uses the platform behavior for overflow.
|
|
|
|
(Other numeric types and behavior are possible using special
|
2019-02-21 00:46:04 +03:00
|
|
|
libraries.)
|
|
|
|
|
2013-08-19 22:55:01 +04:00
|
|
|
In addition to simple values like Integers, Floats, and Booleans,
|
|
|
|
Haxe provides standard library implementations for common data
|
|
|
|
structures like strings, arrays, lists, and maps:
|
|
|
|
*/
|
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// Strings can have double or single quotes.
|
|
|
|
var a_string = "some" + 'string';
|
2013-08-19 22:55:01 +04:00
|
|
|
trace(a_string + " is the value for a_string");
|
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// Strings can be "interpolated" by inserting variables into specific
|
|
|
|
// positions. The string must be single quoted, and the variable must
|
|
|
|
// be preceded with "$". Expressions can be enclosed in ${...}.
|
2013-08-20 08:54:38 +04:00
|
|
|
var x = 1;
|
|
|
|
var an_interpolated_string = 'the value of x is $x';
|
2013-08-24 03:24:35 +04:00
|
|
|
var another_interpolated_string = 'the value of x + 1 is ${x + 1}';
|
2013-08-20 08:54:38 +04:00
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// Strings are immutable, instance methods will return a copy of
|
|
|
|
// parts or all of the string. (See also the StringBuf class).
|
2013-08-19 22:55:01 +04:00
|
|
|
var a_sub_string = a_string.substr(0,4);
|
|
|
|
trace(a_sub_string + " is the value for a_sub_string");
|
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// Regexes are also supported, but there's not enough space here to go
|
|
|
|
// into much detail.
|
2015-03-24 01:21:28 +03:00
|
|
|
var re = ~/foobar/;
|
|
|
|
trace(re.match('foo') + " is the value for (~/foobar/.match('foo')))");
|
2013-08-23 03:38:07 +04:00
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// Arrays are zero-indexed, dynamic, and mutable. Missing values are
|
|
|
|
// defined as null.
|
2013-08-19 22:55:01 +04:00
|
|
|
var a = new Array<String>(); // an array that contains Strings
|
|
|
|
a[0] = 'foo';
|
|
|
|
trace(a.length + " is the value for a.length");
|
|
|
|
a[9] = 'bar';
|
|
|
|
trace(a.length + " is the value for a.length (after modification)");
|
|
|
|
trace(a[3] + " is the value for a[3]"); //null
|
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// Arrays are *generic*, so you can indicate which values they contain
|
|
|
|
// with a type parameter:
|
2013-08-19 22:55:01 +04:00
|
|
|
var a2 = new Array<Int>(); // an array of Ints
|
|
|
|
var a3 = new Array<Array<String>>(); // an Array of Arrays (of Strings).
|
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// Maps are simple key/value data structures. The key and the value
|
|
|
|
// can be of any type.
|
|
|
|
// Here, the keys are strings, and the values are Ints:
|
|
|
|
var m = new Map<String, Int>();
|
2013-08-19 22:55:01 +04:00
|
|
|
m.set('foo', 4);
|
2019-02-21 00:46:04 +03:00
|
|
|
// You can also use array notation:
|
2013-08-19 22:55:01 +04:00
|
|
|
m['bar'] = 5;
|
|
|
|
trace(m.exists('bar') + " is the value for m.exists('bar')");
|
|
|
|
trace(m.get('bar') + " is the value for m.get('bar')");
|
|
|
|
trace(m['bar'] + " is the value for m['bar']");
|
|
|
|
|
2019-09-30 06:29:13 +03:00
|
|
|
var m2 = ['foo' => 4, 'baz' => 6]; // Alternative map syntax
|
2013-08-19 22:55:01 +04:00
|
|
|
trace(m2 + " is the value for m2");
|
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// Remember, you can use type inference. The Haxe compiler will
|
|
|
|
// decide the type of the variable the first time you pass an
|
|
|
|
// argument that sets a type parameter.
|
2013-08-19 22:55:01 +04:00
|
|
|
var m3 = new Map();
|
2013-08-20 01:10:48 +04:00
|
|
|
m3.set(6, 'baz'); // m3 is now a Map<Int,String>
|
2013-08-19 22:55:01 +04:00
|
|
|
trace(m3 + " is the value for m3");
|
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// Haxe has some more common datastructures in the haxe.ds module, such
|
|
|
|
// as List, Stack, and BalancedTree.
|
2013-08-19 22:55:01 +04:00
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Operators
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
trace("***OPERATORS***");
|
|
|
|
|
|
|
|
// basic arithmetic
|
|
|
|
trace((4 + 3) + " is the value for (4 + 3)");
|
|
|
|
trace((5 - 1) + " is the value for (5 - 1)");
|
|
|
|
trace((2 * 4) + " is the value for (2 * 4)");
|
2019-02-21 00:46:04 +03:00
|
|
|
// Division always produces Floats.
|
|
|
|
trace((8 / 3) + " is the value for (8 / 3) (a Float)");
|
2013-08-19 22:55:01 +04:00
|
|
|
trace((12 % 4) + " is the value for (12 % 4)");
|
|
|
|
|
2019-02-21 01:20:29 +03:00
|
|
|
// basic comparison
|
2013-08-20 01:09:18 +04:00
|
|
|
trace((3 == 2) + " is the value for 3 == 2");
|
|
|
|
trace((3 != 2) + " is the value for 3 != 2");
|
|
|
|
trace((3 > 2) + " is the value for 3 > 2");
|
|
|
|
trace((3 < 2) + " is the value for 3 < 2");
|
|
|
|
trace((3 >= 2) + " is the value for 3 >= 2");
|
|
|
|
trace((3 <= 2) + " is the value for 3 <= 2");
|
2013-08-19 22:55:01 +04:00
|
|
|
|
2013-08-23 03:38:07 +04:00
|
|
|
// standard bitwise operators
|
2013-08-19 22:55:01 +04:00
|
|
|
/*
|
2019-02-21 01:20:29 +03:00
|
|
|
~ Unary bitwise complement
|
|
|
|
<< Signed left shift
|
|
|
|
>> Signed right shift
|
|
|
|
>>> Unsigned right shift
|
|
|
|
& Bitwise AND
|
|
|
|
^ Bitwise exclusive OR
|
|
|
|
| Bitwise inclusive OR
|
2013-08-19 22:55:01 +04:00
|
|
|
*/
|
2019-09-30 06:21:42 +03:00
|
|
|
|
2013-08-19 22:55:01 +04:00
|
|
|
var i = 0;
|
2019-09-30 06:21:42 +03:00
|
|
|
trace("Pre-/Post- Increments and Decrements");
|
2019-02-21 00:46:04 +03:00
|
|
|
trace(i++); // i = 1. Post-Increment
|
|
|
|
trace(++i); // i = 2. Pre-Increment
|
|
|
|
trace(i--); // i = 1. Post-Decrement
|
|
|
|
trace(--i); // i = 0. Pre-Decrement
|
|
|
|
|
2013-08-19 22:55:01 +04:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Control Structures
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
trace("***CONTROL STRUCTURES***");
|
|
|
|
|
|
|
|
// if statements
|
|
|
|
var j = 10;
|
2019-02-21 01:20:29 +03:00
|
|
|
if (j == 10) {
|
2013-08-19 22:55:01 +04:00
|
|
|
trace("this is printed");
|
2019-02-21 01:20:29 +03:00
|
|
|
} else if (j > 10) {
|
2013-08-19 22:55:01 +04:00
|
|
|
trace("not greater than 10, so not printed");
|
|
|
|
} else {
|
|
|
|
trace("also not printed.");
|
|
|
|
}
|
|
|
|
|
2013-08-21 08:25:34 +04:00
|
|
|
// there is also a "ternary" if:
|
2019-02-21 01:20:29 +03:00
|
|
|
(j == 10) ? trace("equals 10") : trace("not equals 10");
|
2013-08-21 08:25:34 +04:00
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// Finally, there is another form of control structure that operates
|
|
|
|
// at compile time: conditional compilation.
|
2013-08-22 09:07:32 +04:00
|
|
|
#if neko
|
|
|
|
trace('hello from neko');
|
|
|
|
#elseif js
|
|
|
|
trace('hello from js');
|
|
|
|
#else
|
|
|
|
trace('hello from another platform!');
|
|
|
|
#end
|
2019-02-21 00:46:04 +03:00
|
|
|
|
|
|
|
// The compiled code will change depending on the platform target.
|
|
|
|
// Since we're compiling for neko (-x or -neko), we only get the neko
|
|
|
|
// greeting.
|
2013-08-22 09:07:32 +04:00
|
|
|
|
|
|
|
|
2013-08-19 22:55:01 +04:00
|
|
|
trace("Looping and Iteration");
|
|
|
|
|
|
|
|
// while loop
|
|
|
|
var k = 0;
|
2019-02-21 01:20:29 +03:00
|
|
|
while (k < 100) {
|
2013-08-19 22:55:01 +04:00
|
|
|
// trace(counter); // will print out numbers 0-99
|
2013-08-20 01:09:18 +04:00
|
|
|
k++;
|
2013-08-19 22:55:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// do-while loop
|
2019-09-30 06:29:13 +03:00
|
|
|
var l = 0;
|
2019-02-21 00:46:04 +03:00
|
|
|
do {
|
2013-08-19 22:55:01 +04:00
|
|
|
trace("do statement always runs at least once");
|
2015-05-19 19:17:35 +03:00
|
|
|
} while (l > 0);
|
2013-08-19 22:55:01 +04:00
|
|
|
|
|
|
|
// for loop
|
2019-02-21 00:46:04 +03:00
|
|
|
// There is no c-style for loop in Haxe, because they are prone
|
|
|
|
// to error, and not necessary. Instead, Haxe has a much simpler
|
|
|
|
// and safer version that uses Iterators (more on those later).
|
2019-02-21 01:20:29 +03:00
|
|
|
var m = [1, 2, 3];
|
2019-02-21 00:46:04 +03:00
|
|
|
for (val in m) {
|
2013-08-19 22:55:01 +04:00
|
|
|
trace(val + " is the value for val in the m array");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that you can iterate on an index using a range
|
|
|
|
// (more on ranges later as well)
|
|
|
|
var n = ['foo', 'bar', 'baz'];
|
2019-02-21 00:46:04 +03:00
|
|
|
for (val in 0...n.length) {
|
2015-05-20 15:06:05 +03:00
|
|
|
trace(val + " is the value for val (an index for n)");
|
2013-08-19 22:55:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
trace("Array Comprehensions");
|
|
|
|
|
|
|
|
// Array comprehensions give you the ability to iterate over arrays
|
|
|
|
// while also creating filters and modifications.
|
2013-08-20 01:09:18 +04:00
|
|
|
var filtered_n = [for (val in n) if (val != "foo") val];
|
2013-08-19 22:55:01 +04:00
|
|
|
trace(filtered_n + " is the value for filtered_n");
|
2013-08-20 08:54:38 +04:00
|
|
|
|
2013-08-20 01:09:18 +04:00
|
|
|
var modified_n = [for (val in n) val += '!'];
|
2013-08-19 22:55:01 +04:00
|
|
|
trace(modified_n + " is the value for modified_n");
|
2013-08-20 08:54:38 +04:00
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
var filtered_and_modified_n
|
|
|
|
= [for (val in n) if (val != "foo") val += "!"];
|
|
|
|
trace(filtered_and_modified_n
|
|
|
|
+ " is the value for filtered_and_modified_n");
|
|
|
|
|
2013-08-20 08:54:38 +04:00
|
|
|
|
2013-08-19 22:55:01 +04:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Switch Statements (Value Type)
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
trace("***SWITCH STATEMENTS (VALUE TYPES)***");
|
|
|
|
|
|
|
|
/*
|
|
|
|
Switch statements in Haxe are very powerful. In addition to working
|
|
|
|
on basic values like strings and ints, they can also work on the
|
|
|
|
generalized algebraic data types in enums (more on enums later).
|
2019-05-10 23:30:29 +03:00
|
|
|
Here are some basic value examples for now:
|
2013-08-19 22:55:01 +04:00
|
|
|
*/
|
2013-08-21 08:25:34 +04:00
|
|
|
var my_dog_name = "fido";
|
|
|
|
var favorite_thing = "";
|
2019-09-30 05:57:33 +03:00
|
|
|
switch (my_dog_name) {
|
2013-08-21 08:25:34 +04:00
|
|
|
case "fido" : favorite_thing = "bone";
|
|
|
|
case "rex" : favorite_thing = "shoe";
|
|
|
|
case "spot" : favorite_thing = "tennis ball";
|
|
|
|
default : favorite_thing = "some unknown treat";
|
2019-02-21 00:46:04 +03:00
|
|
|
// same as default:
|
|
|
|
// case _ : favorite_thing = "some unknown treat";
|
2013-08-19 22:55:01 +04:00
|
|
|
}
|
2019-02-21 00:46:04 +03:00
|
|
|
// The "_" case above is a "wildcard" value that will match anything.
|
2013-08-19 22:55:01 +04:00
|
|
|
|
2017-06-12 00:45:03 +03:00
|
|
|
trace("My dog's name is " + my_dog_name
|
2013-08-19 22:55:01 +04:00
|
|
|
+ ", and his favorite thing is a: "
|
|
|
|
+ favorite_thing);
|
2013-08-20 08:54:38 +04:00
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
|
2013-08-19 23:24:18 +04:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Expression Statements
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
trace("***EXPRESSION STATEMENTS***");
|
2013-08-20 08:54:38 +04:00
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// Haxe control statements are very powerful because every statement
|
|
|
|
// is also an expression, consider:
|
2013-08-20 08:54:38 +04:00
|
|
|
|
2013-08-19 23:24:18 +04:00
|
|
|
// if statements
|
2015-03-24 01:21:28 +03:00
|
|
|
var k = if (true) 10 else 20;
|
2013-08-20 08:54:38 +04:00
|
|
|
|
2015-05-20 15:06:05 +03:00
|
|
|
trace("k equals ", k); // outputs 10
|
2013-08-20 08:54:38 +04:00
|
|
|
|
2019-09-30 05:57:33 +03:00
|
|
|
var other_favorite_thing = switch (my_dog_name) {
|
2013-08-21 08:25:34 +04:00
|
|
|
case "fido" : "teddy";
|
|
|
|
case "rex" : "stick";
|
|
|
|
case "spot" : "football";
|
|
|
|
default : "some unknown treat";
|
2013-08-19 23:24:18 +04:00
|
|
|
}
|
2013-08-20 08:54:38 +04:00
|
|
|
|
2017-06-12 00:45:03 +03:00
|
|
|
trace("My dog's name is " + my_dog_name
|
2013-08-19 23:24:18 +04:00
|
|
|
+ ", and his other favorite thing is a: "
|
|
|
|
+ other_favorite_thing);
|
2013-08-20 08:54:38 +04:00
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
|
2013-08-19 22:55:01 +04:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Converting Value Types
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2013-08-22 09:29:31 +04:00
|
|
|
trace("***CONVERTING VALUE TYPES***");
|
2013-08-20 08:54:38 +04:00
|
|
|
|
2013-08-19 22:55:01 +04:00
|
|
|
// You can convert strings to ints fairly easily.
|
|
|
|
|
|
|
|
// string to integer
|
2019-02-21 01:20:29 +03:00
|
|
|
Std.parseInt("0"); // returns 0
|
|
|
|
Std.parseFloat("0.4"); // returns 0.4
|
2013-08-19 22:55:01 +04:00
|
|
|
|
|
|
|
// integer to string
|
2019-02-21 01:20:29 +03:00
|
|
|
Std.string(0); // returns "0"
|
2013-08-19 22:55:01 +04:00
|
|
|
// concatenation with strings will auto-convert to string.
|
2019-02-21 01:20:29 +03:00
|
|
|
0 + ""; // returns "0"
|
|
|
|
true + ""; // returns "true"
|
2013-08-19 22:55:01 +04:00
|
|
|
// See documentation for parsing in Std for more details.
|
|
|
|
|
2013-08-22 07:33:55 +04:00
|
|
|
|
2013-08-22 09:29:31 +04:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Dealing with Types
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/*
|
|
|
|
As mentioned before, Haxe is a statically typed language. All in
|
|
|
|
all, static typing is a wonderful thing. It enables
|
2013-08-23 03:38:07 +04:00
|
|
|
precise autocompletions, and can be used to thoroughly check the
|
|
|
|
correctness of a program. Plus, the Haxe compiler is super fast.
|
2013-08-22 09:29:31 +04:00
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
*HOWEVER*, there are times when you just wish the compiler would
|
|
|
|
let something slide, and not throw a type error in a given case.
|
2013-08-22 09:29:31 +04:00
|
|
|
|
2013-08-22 21:58:46 +04:00
|
|
|
To do this, Haxe has two separate keywords. The first is the
|
2013-08-22 09:29:31 +04:00
|
|
|
"Dynamic" type:
|
|
|
|
*/
|
|
|
|
var dyn: Dynamic = "any type of variable, such as this string";
|
2013-08-22 07:33:55 +04:00
|
|
|
|
2013-08-22 09:29:31 +04:00
|
|
|
/*
|
2013-08-22 21:58:46 +04:00
|
|
|
All that you know for certain with a Dynamic variable is that the
|
|
|
|
compiler will no longer worry about what type it is. It is like a
|
2013-08-22 09:29:31 +04:00
|
|
|
wildcard variable: You can pass it instead of any variable type,
|
2013-08-22 21:58:46 +04:00
|
|
|
and you can assign any variable type you want.
|
2013-08-22 09:29:31 +04:00
|
|
|
|
2013-08-23 03:38:07 +04:00
|
|
|
The other more extreme option is the "untyped" keyword:
|
2013-08-22 09:29:31 +04:00
|
|
|
*/
|
2019-02-21 00:46:04 +03:00
|
|
|
untyped {
|
2019-02-21 01:20:29 +03:00
|
|
|
var x:Int = 'foo'; // This can't be right!
|
|
|
|
var y:String = 4; // Madness!
|
2019-02-21 00:46:04 +03:00
|
|
|
}
|
2013-08-22 09:29:31 +04:00
|
|
|
|
|
|
|
/*
|
2013-08-22 21:58:46 +04:00
|
|
|
The untyped keyword operates on entire *blocks* of code, skipping
|
2013-08-22 09:29:31 +04:00
|
|
|
any type checks that might be otherwise required. This keyword should
|
|
|
|
be used very sparingly, such as in limited conditionally-compiled
|
2017-08-23 11:14:39 +03:00
|
|
|
situations where type checking is a hindrance.
|
2013-08-22 09:29:31 +04:00
|
|
|
|
2013-08-22 21:58:46 +04:00
|
|
|
In general, skipping type checks is *not* recommended. Use the
|
2013-08-23 03:38:07 +04:00
|
|
|
enum, inheritance, or structural type models in order to help ensure
|
|
|
|
the correctness of your program. Only when you're certain that none
|
|
|
|
of the type models work should you resort to "Dynamic" or "untyped".
|
2013-08-22 09:29:31 +04:00
|
|
|
*/
|
2013-08-22 07:33:55 +04:00
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
|
2013-08-19 22:55:01 +04:00
|
|
|
//////////////////////////////////////////////////////////////////
|
2013-08-20 08:54:38 +04:00
|
|
|
// Basic Object Oriented Programming
|
2013-08-19 22:55:01 +04:00
|
|
|
//////////////////////////////////////////////////////////////////
|
2013-08-20 08:54:38 +04:00
|
|
|
trace("***BASIC OBJECT ORIENTED PROGRAMMING***");
|
2013-08-19 22:55:01 +04:00
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// Create an instance of FooClass. The classes for this are at the
|
|
|
|
// end of the file.
|
2013-08-24 03:24:35 +04:00
|
|
|
var foo_instance = new FooClass(3);
|
2013-08-20 08:54:38 +04:00
|
|
|
|
2013-08-19 22:55:01 +04:00
|
|
|
// read the public variable normally
|
2019-02-21 00:46:04 +03:00
|
|
|
trace(foo_instance.public_any
|
|
|
|
+ " is the value for foo_instance.public_any");
|
2013-08-19 22:55:01 +04:00
|
|
|
|
|
|
|
// we can read this variable
|
2019-02-21 00:46:04 +03:00
|
|
|
trace(foo_instance.public_read
|
|
|
|
+ " is the value for foo_instance.public_read");
|
|
|
|
// but not write it; this will throw an error if uncommented:
|
|
|
|
// foo_instance.public_read = 4;
|
2013-08-24 03:24:35 +04:00
|
|
|
// trace(foo_instance.public_write); // as will this.
|
2013-08-19 22:55:01 +04:00
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// Calls the toString method:
|
2015-05-20 15:06:05 +03:00
|
|
|
trace(foo_instance + " is the value for foo_instance");
|
2015-05-20 15:29:05 +03:00
|
|
|
// same thing:
|
2019-02-21 00:46:04 +03:00
|
|
|
trace(foo_instance.toString()
|
|
|
|
+ " is the value for foo_instance.toString()");
|
2013-08-19 22:55:01 +04:00
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// The foo_instance has the "FooClass" type, while acceptBarInstance
|
|
|
|
// has the BarClass type. However, since FooClass extends BarClass, it
|
|
|
|
// is accepted.
|
2013-08-24 03:24:35 +04:00
|
|
|
BarClass.acceptBarInstance(foo_instance);
|
2013-08-22 09:07:32 +04:00
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// The classes below have some more advanced examples, the "example()"
|
|
|
|
// method will just run them here.
|
2013-08-22 09:07:32 +04:00
|
|
|
SimpleEnumTest.example();
|
|
|
|
ComplexEnumTest.example();
|
|
|
|
TypedefsAndStructuralTypes.example();
|
2013-08-22 09:36:45 +04:00
|
|
|
UsingExample.example();
|
2013-08-19 22:55:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// This is the "child class" of the main LearnHaxe3 Class.
|
|
|
|
class FooClass extends BarClass implements BarInterface {
|
2013-08-19 22:55:01 +04:00
|
|
|
public var public_any:Int; // public variables are accessible anywhere
|
2015-05-20 15:06:05 +03:00
|
|
|
public var public_read (default, null): Int; // enable only public read
|
|
|
|
public var public_write (null, default): Int; // or only public write
|
2019-02-21 00:46:04 +03:00
|
|
|
// Use this style to enable getters/setters:
|
|
|
|
public var property (get, set): Int;
|
2013-08-19 22:55:01 +04:00
|
|
|
|
|
|
|
// private variables are not available outside the class.
|
2013-08-20 08:54:38 +04:00
|
|
|
// see @:allow for ways around this.
|
2013-08-19 22:55:01 +04:00
|
|
|
var _private:Int; // variables are private if they are not marked public
|
|
|
|
|
|
|
|
// a public constructor
|
2019-02-21 00:46:04 +03:00
|
|
|
public function new(arg:Int) {
|
2015-05-20 15:29:05 +03:00
|
|
|
// call the constructor of the parent object, since we extended BarClass:
|
2015-05-20 15:06:05 +03:00
|
|
|
super();
|
2013-08-19 22:55:01 +04:00
|
|
|
|
2015-05-20 15:06:05 +03:00
|
|
|
this.public_any = 0;
|
2013-08-19 22:55:01 +04:00
|
|
|
this._private = arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
// getter for _private
|
2013-08-20 00:56:01 +04:00
|
|
|
function get_property() : Int {
|
2013-08-19 22:55:01 +04:00
|
|
|
return _private;
|
|
|
|
}
|
|
|
|
|
|
|
|
// setter for _private
|
2013-08-20 01:09:18 +04:00
|
|
|
function set_property(val:Int) : Int {
|
2013-08-19 22:55:01 +04:00
|
|
|
_private = val;
|
2013-08-20 01:09:18 +04:00
|
|
|
return val;
|
2013-08-19 22:55:01 +04:00
|
|
|
}
|
2013-08-20 08:54:38 +04:00
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// Special function that is called whenever an instance is cast to a string.
|
|
|
|
public function toString() {
|
2013-08-19 22:55:01 +04:00
|
|
|
return _private + " with toString() method!";
|
|
|
|
}
|
|
|
|
|
2013-08-20 08:54:38 +04:00
|
|
|
// this class needs to have this function defined, since it implements
|
2013-08-24 03:24:35 +04:00
|
|
|
// the BarInterface interface.
|
2019-02-21 00:46:04 +03:00
|
|
|
public function baseFunction(x: Int) : String {
|
2013-08-19 22:55:01 +04:00
|
|
|
// convert the int to string automatically
|
|
|
|
return x + " was passed into baseFunction!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// A simple class to extend.
|
2013-08-24 03:24:35 +04:00
|
|
|
class BarClass {
|
2013-08-19 22:55:01 +04:00
|
|
|
var base_variable:Int;
|
2019-02-21 00:46:04 +03:00
|
|
|
public function new() {
|
2013-08-19 22:55:01 +04:00
|
|
|
base_variable = 4;
|
|
|
|
}
|
2019-02-21 00:46:04 +03:00
|
|
|
public static function acceptBarInstance(b:BarClass) {}
|
2013-08-19 22:55:01 +04:00
|
|
|
}
|
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// A simple interface to implement
|
2019-02-21 01:20:29 +03:00
|
|
|
interface BarInterface {
|
2013-08-19 22:55:01 +04:00
|
|
|
public function baseFunction(x:Int):String;
|
|
|
|
}
|
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
|
2013-08-22 07:33:55 +04:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Enums and Switch Statements
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// Enums in Haxe are very powerful. In their simplest form, enums
|
|
|
|
// are a type with a limited number of states:
|
2013-08-22 07:33:55 +04:00
|
|
|
enum SimpleEnum {
|
|
|
|
Foo;
|
|
|
|
Bar;
|
|
|
|
Baz;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here's a class that uses it:
|
2019-02-21 00:46:04 +03:00
|
|
|
class SimpleEnumTest {
|
|
|
|
public static function example() {
|
|
|
|
// You can specify the "full" name,
|
|
|
|
var e_explicit:SimpleEnum = SimpleEnum.Foo;
|
2013-08-22 07:33:55 +04:00
|
|
|
var e = Foo; // but inference will work as well.
|
2019-09-30 05:57:33 +03:00
|
|
|
switch (e) {
|
2013-08-22 07:33:55 +04:00
|
|
|
case Foo: trace("e was Foo");
|
|
|
|
case Bar: trace("e was Bar");
|
|
|
|
case Baz: trace("e was Baz"); // comment this line to throw an error.
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
This doesn't seem so different from simple value switches on strings.
|
|
|
|
However, if we don't include *all* of the states, the compiler will
|
|
|
|
complain. You can try it by commenting out a line above.
|
|
|
|
|
|
|
|
You can also specify a default for enum switches as well:
|
|
|
|
*/
|
2019-09-30 05:57:33 +03:00
|
|
|
switch (e) {
|
2013-08-22 07:33:55 +04:00
|
|
|
case Foo: trace("e was Foo again");
|
|
|
|
default : trace("default works here too");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// Enums go much further than simple states, we can also enumerate
|
2019-02-21 01:20:29 +03:00
|
|
|
// *constructors*, but we'll need a more complex enum example.
|
2019-02-21 00:46:04 +03:00
|
|
|
enum ComplexEnum {
|
2013-08-22 07:33:55 +04:00
|
|
|
IntEnum(i:Int);
|
|
|
|
MultiEnum(i:Int, j:String, k:Float);
|
|
|
|
SimpleEnumEnum(s:SimpleEnum);
|
|
|
|
ComplexEnumEnum(c:ComplexEnum);
|
|
|
|
}
|
2013-08-22 09:07:32 +04:00
|
|
|
// Note: The enum above can include *other* enums as well, including itself!
|
2015-03-24 12:37:49 +03:00
|
|
|
// Note: This is what's called *Algebraic data type* in some other languages.
|
2013-08-22 07:33:55 +04:00
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
class ComplexEnumTest {
|
|
|
|
public static function example() {
|
2013-08-22 07:33:55 +04:00
|
|
|
var e1:ComplexEnum = IntEnum(4); // specifying the enum parameter
|
2019-02-21 00:46:04 +03:00
|
|
|
// Now we can switch on the enum, as well as extract any parameters
|
2019-05-10 23:30:29 +03:00
|
|
|
// it might have had.
|
2019-09-30 05:57:33 +03:00
|
|
|
switch (e1) {
|
2013-08-22 09:07:32 +04:00
|
|
|
case IntEnum(x) : trace('$x was the parameter passed to e1');
|
2013-08-22 07:33:55 +04:00
|
|
|
default: trace("Shouldn't be printed");
|
|
|
|
}
|
|
|
|
|
2013-08-22 09:07:32 +04:00
|
|
|
// another parameter here that is itself an enum... an enum enum?
|
|
|
|
var e2 = SimpleEnumEnum(Foo);
|
2019-09-30 05:57:33 +03:00
|
|
|
switch (e2){
|
2013-08-22 07:33:55 +04:00
|
|
|
case SimpleEnumEnum(s): trace('$s was the parameter passed to e2');
|
|
|
|
default: trace("Shouldn't be printed");
|
|
|
|
}
|
|
|
|
|
2013-08-22 09:07:32 +04:00
|
|
|
// enums all the way down
|
|
|
|
var e3 = ComplexEnumEnum(ComplexEnumEnum(MultiEnum(4, 'hi', 4.3)));
|
2019-09-30 05:57:33 +03:00
|
|
|
switch (e3) {
|
2019-02-21 00:46:04 +03:00
|
|
|
// You can look for certain nested enums by specifying them
|
|
|
|
// explicitly:
|
2013-08-22 07:37:32 +04:00
|
|
|
case ComplexEnumEnum(ComplexEnumEnum(MultiEnum(i,j,k))) : {
|
2013-08-22 07:33:55 +04:00
|
|
|
trace('$i, $j, and $k were passed into this nested monster');
|
|
|
|
}
|
|
|
|
default: trace("Shouldn't be printed");
|
|
|
|
}
|
2019-02-21 00:46:04 +03:00
|
|
|
// Check out "generalized algebraic data types" (GADT) for more details
|
|
|
|
// on why these are so great.
|
2013-08-22 07:33:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-22 09:07:32 +04:00
|
|
|
class TypedefsAndStructuralTypes {
|
2019-02-21 01:20:29 +03:00
|
|
|
public static function example() {
|
2019-02-21 00:46:04 +03:00
|
|
|
// Here we're going to use typedef types, instead of base types.
|
|
|
|
// At the top we've declared the type "FooString" to mean a "String" type.
|
2013-08-22 09:07:32 +04:00
|
|
|
var t1:FooString = "some string";
|
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
// We can use typedefs for "structural types" as well. These types are
|
|
|
|
// defined by their field structure, not by class inheritance. Here's
|
|
|
|
// an anonymous object with a String field named "foo":
|
2013-08-22 21:58:46 +04:00
|
|
|
var anon_obj = { foo: 'hi' };
|
2013-08-22 09:07:32 +04:00
|
|
|
|
|
|
|
/*
|
2013-08-22 21:58:46 +04:00
|
|
|
The anon_obj variable doesn't have a type declared, and is an
|
|
|
|
anonymous object according to the compiler. However, remember back at
|
|
|
|
the top where we declared the FooObj typedef? Since anon_obj matches
|
|
|
|
that structure, we can use it anywhere that a "FooObject" type is
|
|
|
|
expected.
|
2013-08-22 09:07:32 +04:00
|
|
|
*/
|
2019-02-21 00:46:04 +03:00
|
|
|
var f = function(fo:FooObject) {
|
2013-08-22 09:11:01 +04:00
|
|
|
trace('$fo was passed in to this function');
|
|
|
|
}
|
2013-08-22 21:58:46 +04:00
|
|
|
f(anon_obj); // call the FooObject signature function with anon_obj.
|
2013-08-22 09:07:32 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
Note that typedefs can have optional fields as well, marked with "?"
|
|
|
|
|
|
|
|
typedef OptionalFooObj = {
|
|
|
|
?optionalString: String,
|
|
|
|
requiredInt: Int
|
|
|
|
}
|
|
|
|
|
|
|
|
Typedefs work well with conditional compilation. For instance,
|
|
|
|
we could have included this at the top of the file:
|
|
|
|
|
|
|
|
#if( js )
|
|
|
|
typedef Surface = js.html.CanvasRenderingContext2D;
|
|
|
|
#elseif( nme )
|
|
|
|
typedef Surface = nme.display.Graphics;
|
|
|
|
#elseif( !flash9 )
|
|
|
|
typedef Surface = flash8.MovieClip;
|
|
|
|
#elseif( java )
|
|
|
|
typedef Surface = java.awt.geom.GeneralPath;
|
|
|
|
#end
|
|
|
|
|
2019-02-21 00:46:04 +03:00
|
|
|
That would give us a single "Surface" type to work with across
|
|
|
|
all of those platforms.
|
2019-09-30 05:42:01 +03:00
|
|
|
*/
|
2013-08-22 09:07:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class UsingExample {
|
|
|
|
public static function example() {
|
|
|
|
/*
|
|
|
|
The "using" import keyword is a special type of class import that
|
|
|
|
alters the behavior of any static methods in the class.
|
|
|
|
|
|
|
|
In this file, we've applied "using" to "StringTools", which contains
|
|
|
|
a number of static methods for dealing with String types.
|
|
|
|
*/
|
|
|
|
trace(StringTools.endsWith("foobar", "bar") + " should be true!");
|
|
|
|
|
|
|
|
/*
|
|
|
|
With a "using" import, the first argument type is extended with the
|
|
|
|
method. What does that mean? Well, since "endsWith" has a first
|
|
|
|
argument type of "String", that means all String types now have the
|
|
|
|
"endsWith" method:
|
|
|
|
*/
|
|
|
|
trace("foobar".endsWith("bar") + " should be true!");
|
|
|
|
|
|
|
|
/*
|
|
|
|
This technique enables a good deal of expression for certain types,
|
|
|
|
while limiting the scope of modifications to a single file.
|
|
|
|
|
|
|
|
Note that the String instance is *not* modified in the run time.
|
|
|
|
The newly attached method is not really part of the attached
|
|
|
|
instance, and the compiler still generates code equivalent to a
|
|
|
|
static method.
|
|
|
|
*/
|
2019-09-30 05:42:01 +03:00
|
|
|
}
|
2013-08-22 09:07:32 +04:00
|
|
|
}
|
2013-08-19 22:55:01 +04:00
|
|
|
```
|
|
|
|
|
2013-08-22 09:07:32 +04:00
|
|
|
We're still only scratching the surface here of what Haxe can do. For a formal
|
2018-08-30 06:52:31 +03:00
|
|
|
overview of all Haxe features, see the [manual](https://haxe.org/manual) and
|
|
|
|
the [API docs](https://api.haxe.org/). For a comprehensive directory of available
|
|
|
|
third-party Haxe libraries, see [Haxelib](https://lib.haxe.org/).
|
2013-08-22 09:07:32 +04:00
|
|
|
|
|
|
|
For more advanced topics, consider checking out:
|
|
|
|
|
2018-08-30 06:19:28 +03:00
|
|
|
* [Abstract types](https://haxe.org/manual/types-abstract.html)
|
|
|
|
* [Macros](https://haxe.org/manual/macro.html)
|
|
|
|
* [Compiler Features](https://haxe.org/manual/cr-features.html)
|
2013-08-22 09:07:32 +04:00
|
|
|
|
|
|
|
|
2018-08-30 06:19:28 +03:00
|
|
|
Finally, please join us on [the Haxe forum](https://community.haxe.org/),
|
|
|
|
on IRC [#haxe on
|
|
|
|
freenode](http://webchat.freenode.net/), or on the
|
|
|
|
[Haxe Gitter chat](https://gitter.im/HaxeFoundation/haxe).
|