Skip to content

Instantly share code, notes, and snippets.

View jasononeil's full-sized avatar

Jason O'Neil jasononeil

View GitHub Profile
@jasononeil
jasononeil / WebSocketTest.hx
Created April 5, 2013 10:25
A haxe port of the WebSockets demo here: http://www.websocket.org/echo.html Works with Haxe3.
// Compile with: haxe -main WebSocketTest.hx -js WebSocketTest.js
// Doing your imports like this makes it much easier to port with existing Javascript code.
import js.Browser.window;
import js.Browser.document;
import js.html.*;
class WebSocketTest
{
static var wsUri = "ws://echo.websocket.org/";
@jasononeil
jasononeil / CustomSerialisation.hx
Created April 12, 2013 12:54
A test of custom serialisation in Haxe when using sys.db.Object objects. Results in file below * Using a custom serialiser for SPOD can reduce speeds by more than half. * String size is also reduced, which is good if you're sending over HTTP * Have a build macro append a static array containing the names of the fields to serialise.
import sys.db.Types;
using Lambda;
/**
* What this class shows:
*
* Using a custom serialiser for SPOD can reduce speeds by more than half.
* Have a build macro append a static array containing the names of the fields to serialise.
*/
@jasononeil
jasononeil / DispatchTest.hx
Last active December 17, 2015 20:19
A sample to test some more complex routing options of haxe.web.Dispatch. Run with `haxe -x DispatchTest.hx` This is the sample code for this tutorial / blog post: http://jasononeil.com.au/2013/05/29/creating-complex-url-routing-schemes-with-haxe-web-dispatch/
import haxe.ds.StringMap;
import haxe.web.Dispatch;
import haxe.web.Dispatch.DispatchError;
#if sys
import Sys.println;
import Sys.print;
#else
import haxe.Log.trace in println;
import haxe.Log.trace in print;
@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;
@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 / 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 / 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 / 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 / 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 / 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