Skip to content

Instantly share code, notes, and snippets.

View jasononeil's full-sized avatar

Jason O'Neil jasononeil

View GitHub Profile
@jasononeil
jasononeil / AbstractExample.hx
Last active December 27, 2015 15:48
Really simple example of abstracts - creating an "extension" of AnchorElement with a custom constructor. Uses all inline code, and shows how implicit casts can be used when interacting with API that expects the underlying type.
// Compile with `haxe -js AbstractExample.js -dce full -main AbstractExample.hx`
import js.html.AnchorElement;
class AbstractExample {
static function main() {
var myAnchor = new MyAnchor( "http://google.com", "Click here!" );
expectsAnchor( myAnchor ); // Notice how it automatically casts back down to AnchorElement
}
@jasononeil
jasononeil / TraceMemberMethod.hx
Created September 15, 2013 10:13
In Haxe, a member trace() method does not have priority over the haxe.Log.trace()
import haxe.PosInfos;
class TraceMemberMethod
{
static function main() new TraceMemberMethod();
public function new() {
messages = [];
trace( "hey" ); // Uses haxe.Log.trace()
@jasononeil
jasononeil / Module1.hx
Created September 6, 2013 02:16
**Compile to separate JS Files in Haxe** In this example 1) SharedCode is compiled to sharedcode.js and must be loaded first 2) Both Module1 and Module2 reference SharedCode, but do not duplicate it's code. Look at the resulting Javascript to see the output. You would have to ensure sharedcode.js is loaded before either of the module javascript …
class Module1
{
static function main() {
SharedCode.greet("Jason");
}
}
@jasononeil
jasononeil / Mysql.hx
Created August 28, 2013 09:49
Attempt to implement a MySQL class for Haxe Java based on waneck's branch: https://github.com/waneck/haxe/tree/java-sql
package sys.db;
@:coreApi class Mysql {
static var init = false;
/**
Opens a new MySQL connection on the specified path.
Note that you will need a MySQL JDBC driver.
"socket" option currently not supported
@jasononeil
jasononeil / Cast.hx
Last active December 21, 2015 05:29
An Abstract (A) that is Iterable<T> cannot be automatically cast to an Abstract (B) that has a "@:from" cast for Iterable<T>
class Cast
{
static function main()
{
var array = [0,1,2,3];
var list = Lambda.list( array );
var map = [ "a"=>0, "b"=>1, "c"=>2 ];
var stringMap:haxe.ds.StringMap<Int> = map;
var myAbstract:AbstractIterator<Int> = array;
var myAbstract2:AbstractIterator2<Int> = array;
@jasononeil
jasononeil / ArrayAccessTest.hx
Last active December 19, 2015 06:09
Haxe Abstracts: Array Access with different key types representing different functionality
class ArrayAccessTest {
static function main() {
var myArray:MyArray<Int> = [0,10,20];
trace ( myArray[1] );
trace ( myArray["one"] );
trace ( myArray[["one","two","three"]] );
}
}
abstract MyArray<T>(Array<T>) from Array<T> to Array<T> {
@jasononeil
jasononeil / BuildInMacroTests.hx
Created June 24, 2013 07:49
Minimal reproducible test for this error message: /usr/lib/haxe/std/sys/db/Object.hx:29: characters 2-11 : You cannot use @:build inside a macro : make sure that your enum is not used in macro
import haxe.macro.Expr;
import haxe.macro.Context;
import neko.Lib;
class BuildInMacroTests {
public static function main() {
var s:MyModel = new MyModel();
Formatter.__Output_Formatter( s );
}
}
@jasononeil
jasononeil / MacroTest.hx
Created June 23, 2013 05:41
Demonstration of blatantly copying fields to a new type not working...
import haxe.macro.Expr;
import haxe.macro.Type;
import haxe.macro.Context;
class MacroTest
{
macro public function build():Array<Field>
{
trace ("Build macro");
@jasononeil
jasononeil / BuildMacro.hx
Last active December 18, 2015 05:39
Macro example for EzeQL_ - create ".hx" files at macro time, and then go ahead and use them in your code.
import haxe.macro.Expr;
import haxe.macro.Context;
import sys.FileSystem;
import sys.io.File;
import haxe.io.Path;
class BuildMacro
{
static function build()
{
@jasononeil
jasononeil / MacroHelpTest.hx
Last active December 18, 2015 03:38
Using a macro as the initialization of one field, and having it base itself on an existing member variable.
import haxe.macro.Expr;
import haxe.macro.Context;
class F
{
macro public static function f(a : Expr, b : Int) : Expr
{
// get the field name as a string, use Pattern Matching or tink macros probably...
var fieldName = switch (a.expr) {
case EConst(CIdent(name)): name;