Skip to content

Instantly share code, notes, and snippets.

View jasononeil's full-sized avatar

Jason O'Neil jasononeil

View GitHub Profile
@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 / 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 / Test.hx
Last active January 2, 2016 16:18
`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.
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 / MethodTyper.hx
Created February 19, 2014 05:11
Method used in a macro to figure out the return type of a member method. Uses tink_macro
/**
Figure out return type of function.
Will first to check if it is explicitly defined.
If not, it attempts to set the function up as a local function expression (EFunction), and then use `Context.typeof( fnExpr )` to evaluate the type.
Will generate a compiler error if member was not a method or method type could not be determined.
**/
static function figureOutReturnTypeOfMemberMethod( member:Member ) {
switch member.getFunction() {
@jasononeil
jasononeil / Http.hx
Created March 5, 2014 22:37
Workaround for Hxssl being broken for Http requests. Drop these 4 files into a package called "haxe", and you can use this as a drop-in workaround for HxSSL breaking regular HTTP port 80 requests
package haxe;
@:access( haxe.Https )
@:access( haxe.Http80 )
class Http {
public var h:IHttp;
public var url(get, set) : String;
function get_url() return h.url;
@jasononeil
jasononeil / Metric.hx
Last active November 10, 2016 11:16
Demonstration of using Haxe abstracts to take care of explicit conversions between different units of measurement, with no performance overhead.
class Metric {
static function main() {
var coinRadius:Millimeters = 12;
var myHeight:Centimeters = 180;
var raceLength:Meters = 200;
var commuteDistance:Kilometers = 23;
diff( coinRadius, myHeight ); // 1.788 meters
diff( raceLength, commuteDistance ); // 22800 meters
sum( commuteDistance, coinRadius ); // 23000.012 meters
@jasononeil
jasononeil / ObjectInit.hx
Created December 2, 2014 01:19
Object initialisations syntax sugar
import haxe.macro.Expr;
import haxe.macro.Context;
using haxe.macro.ExprTools;
class ObjectInit {
macro public static function init( expr:Expr, varsToSet:ExprOf<Dynamic<Dynamic>> ) {
var lines:Array<Expr> = [];
lines.push( macro var __obj_init_tmp = $expr );
switch varsToSet.expr {
case EObjectDecl(fields):
@jasononeil
jasononeil / JS Callback to Future
Last active November 10, 2015 23:14 — forked from kevinresol/ JS Callback to Future
Convert js-style callback to Future, with macro
Convert js-style callback to Future, with macro
@jasononeil
jasononeil / ReflectionCost.hx
Created June 12, 2016 12:24
Demonstrate how much extra code reflection (Reflect.* and Type.*) generate in Haxe code
class ReflectionCost {
public static function main() {
var obj = new ReflectionCost();
#if reflect
Reflect.setField( obj, "name", "Jason" );
Reflect.callMethod( obj, Reflect.field(obj, "printName"), [] );
Type.getInstanceFields( Type.getClass(obj) );
#else
obj.name = "Jason";
obj.printName();
@jasononeil
jasononeil / metrics.txt
Created November 14, 2016 01:18
List of all Sitespeed 4.0 metrics. I couldn't find this list anywhere, and it took me a while to figure out how to generate the file using grunt-sitespeedio (for future reference: set `sitespeedio.default.options.metrics.list=true`). I figure pasting this here will help some other poor souls thanks to the SEO goodness.
browsertime.pageSummary.browserScripts.0.pageinfo.documentHeight
browsertime.pageSummary.browserScripts.0.pageinfo.documentWidth
browsertime.pageSummary.browserScripts.0.pageinfo.responsive
browsertime.pageSummary.browserScripts.0.timings.firstPaint
browsertime.pageSummary.browserScripts.0.timings.fullyLoaded
browsertime.pageSummary.browserScripts.0.timings.navigationTiming.connectEnd
browsertime.pageSummary.browserScripts.0.timings.navigationTiming.connectStart
browsertime.pageSummary.browserScripts.0.timings.navigationTiming.domComplete
browsertime.pageSummary.browserScripts.0.timings.navigationTiming.domContentLoadedEventEnd
browsertime.pageSummary.browserScripts.0.timings.navigationTiming.domContentLoadedEventStart