Skip to content

Instantly share code, notes, and snippets.

View devboy's full-sized avatar

Dominic Graefen devboy

View GitHub Profile
var ints = [1, 2, 3]
var strings = ["a", "bb", "ccc"];
var biggerThan1 = function(x:Int) {
return x > 1;
}
var longerThan1 = function(x:String) {
return x.length > 1;
}
function doMath( x: Float, y: Float, mathOperation: Float -> Float -> Float ) {
return mathOperation(x, y);
}
var addition = function(x: Float, y: Float) {
return x + y;
}
var square = function(x: Float) {
return x * x;
var biggerThan2: Int -> Bool = function(x) {
return x > 2;
}
var getStringXTimes = function(x: Int, string: String) {
var result = "";
for ( i in 0...x )
result += string;
return result;
}
type(getStringXTimes); // Int -> String -> String
var square = function(x: Float) {
return x * x;
}
type(square); // type of square is Float -> Float
function square( x: Float ) {
return x * x;
}
function inferred(x) {
return x;
}
inferred("abc"); // will take the string and return it
inferred(1); // will throw a compiler error because x is typed to String now
var x: String; // type is now set to String
x = "I'm a String"; // this will work
x = 1; // this will cause an error
@devboy
devboy / typeInf.as
Created November 21, 2011 23:38
type inference basics
var x; // type of x is Unknown
x = "I'm a String"; // type of x is set to String
x = 1; // this will cause a compiler error as Int cannot be assigned to String
@devboy
devboy / avoidDynamic
Created November 19, 2011 17:41
Avoiding Dynamic types in haXe
static inline function array_delete_if<T>( array: Array<T>, processor: T -> Bool ):Array<T>
{
for ( item in array )
if ( processor(item) )
array.remove( item );
return array;
}