Skip to content

Instantly share code, notes, and snippets.

Jason O'Neil jasononeil

Block or report user

Report or block jasononeil

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@jasononeil
jasononeil / Test.hx
Last active Jan 2, 2016
`AcceptEither`, a way to accept either one type or another, without resorting to "Dynamic", and still have the compiler type-check everything and make sure you correctly handle every situation.
View Test.hx
class Test {
static function main(){
stringOrInt( "hello" );
stringOrInt( 10 );
}
static function stringOrInt( either:AcceptEither<String,Int> ) {
switch either.type {
case Left(str): trace("string: "+str);
case Right(int): trace("int: "+int);
@jasononeil
jasononeil / AbstractExample.hx
Last active Dec 27, 2015
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.
View AbstractExample.hx
// 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 Sep 15, 2013
In Haxe, a member trace() method does not have priority over the haxe.Log.trace()
View TraceMemberMethod.hx
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 Sep 6, 2013
**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 …
View Module1.hx
class Module1
{
static function main() {
SharedCode.greet("Jason");
}
}
@jasononeil
jasononeil / Mysql.hx
Created Aug 28, 2013
Attempt to implement a MySQL class for Haxe Java based on waneck's branch: https://github.com/waneck/haxe/tree/java-sql
View Mysql.hx
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 Dec 21, 2015
An Abstract (A) that is Iterable<T> cannot be automatically cast to an Abstract (B) that has a "@:from" cast for Iterable<T>
View Cast.hx
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 Dec 19, 2015
Haxe Abstracts: Array Access with different key types representing different functionality
View ArrayAccessTest.hx
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 Jun 24, 2013
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
View BuildInMacroTests.hx
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 Jun 23, 2013
Demonstration of blatantly copying fields to a new type not working...
View MacroTest.hx
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 Dec 18, 2015
Macro example for EzeQL_ - create ".hx" files at macro time, and then go ahead and use them in your code.
View BuildMacro.hx
import haxe.macro.Expr;
import haxe.macro.Context;
import sys.FileSystem;
import sys.io.File;
import haxe.io.Path;
class BuildMacro
{
static function build()
{
You can’t perform that action at this time.