Code Monkey home page Code Monkey logo

haxe-extrafeatures's Introduction

Provides extra features not available in vanilla Haxe. Typically these are features that are applicable to nearly all expressions and favor function-calling/chaining syntax.

This library provides exclusively macro functions, so no additional bloat is added to transpiled code/executable.


[Installation]

Install via haxelib.

haxelib install extra-features

Add this top of the file or import.hx.

using ExtraFeatures;

[Features]

"unpack" (destructuring feature)

Based on JavaScript's object destructuring. Call "unpack" on an expression to assign its fields to new or existing variables of the same names of the fields.

function getObj() return { name: "John", age: 66 };

var name = "Default";
getObj().unpack(name, var age);

assert(name == "John");
assert(age == 66);

 

enum "unpack"

Enums can also be unpack-ed to easily extract contained data.

enum FileType {
	Shortcut(path: String);
	File(size: Int);
	Folder(size: Int, fileCount: Int);
}

// ---

var file = File(123);
file.unpack(var size);
assert(size == 123);

Folder(300, 5).unpack(size, var fileCount);
assert(size == 300);

Shortcut("downloads/").unpack(size); // throws error, no "size" data

 

"onlyif"/"unless" (posfix condition feature)

Based on Ruby's postfix conditionals. Append "onlyif" to an expression, and the entire expression will only execute if the condition provided to the "onlyif" evaluates to true at runtime. Long story short, it's an if-statement that can be used at the end of a statement for aesthetic purposes. "unless" does the same thing, but the condition must evaluate to false for the expression to run.

// example of use-case with hypothetical "Player" class
var player = new Player();

player.update().onlyif(player.canUpdate());
player.draw().unless(player.invisible());

// at compile-time, these statements are converted to:
//
// if(player.canUpdate()) player.update();
// if(!player.invisible()) player.draw();

 

"with"/"also" (scope functions)

Based on Kotlin's scope functions. These functions use macros to provide zero-cost inline/chain calls to block expressions with the lvalue stored in a variable.

// with vs also
123.with({ it.toString() }); // returns "123"
123.also({ it.toString() }); // returns 123
// example of use-case with hypothetical "Player" class
var player = new Player();

Math.floor(player.x / 100.0).with({
    trace(it);
});

var meters = player.getDistance().with(dist, {
    recordDistance(dist);
    if(dist > 1000) triggerEffect();
    convertDistanceToMeters(dist); // convert to meters and return
});

 

"as"/"retype" (function-call cast)

Based on C# "as" operator. These allow for type casting with a function-like syntax.

var getChild = function(): Base { return new Child(); };

// equivalent to "cast(getChild(), Child)"
var child1 = getChild().as(Child);

// equivalent to "cast getChild()"
var child2: Child = getChild().retype();

haxe-extrafeatures's People

Contributors

somerandev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.