Adi Reddy MoraJekyll2018-02-22T14:45:15+00:00/Adi Reddy Mora/adiinteractive@gmail.com/haxe/checkstyle/static%20analysis/code%20climate/analysis/code-climate-haxe-checkstyle2016-03-29T00:00:00+00:002016-03-29T00:00:00-00:00Adi Reddy Moraadiinteractive@gmail.com
<p>I am happy to announce the partnership with Code Climate to bring static analysis via Haxe Checkstyle to all Haxe users.</p>
<p>Now you can to analyze Haxe projects on <a href="http://codeclimate.com">codeclimate.com</a>, as well as run custom static analysis for Haxe on your command line with their new CLI tool.</p>
<p><img src="/images/codeclimate-platform.png" alt="codeclimate" /></p>
<p>If you are not familiar with Code Climate, it’s a static analysis tool for monitoring the health of your code.
It supports a whole host of languages and frameworks – and it’s all open source!</p>
<p>It lets you configure your analysis via a .codeclimate.yml file in the root of your repository.</p>
<p><a href="https://docs.codeclimate.com/docs/haxe-checkstyle">Click here</a> for more information.</p>
<p><a href="/haxe/checkstyle/static%20analysis/code%20climate/analysis/code-climate-haxe-checkstyle">Haxe Checkstyle Code Climate Integration</a> was originally published by Adi Reddy Mora at <a href="">Adi Reddy Mora</a> on March 29, 2016.</p>/haxe/web/workers/multi-threading/javascript/web-workers2015-11-17T00:00:00+00:002015-11-17T00:00:00-00:00Adi Reddy Moraadiinteractive@gmail.com
<p>I recently started exploring web workers in JavaScript and found them really useful to help boost the general performance of web apps.</p>
<h3 id="the-problem">The Problem</h3>
<p>JavaScript apps are single threaded and can be unresponsive or degrade performance if the app has code that blocks for a long duration. The consequences can be unpleasant page freezes where the user can’t interact with your application.</p>
<h3 id="advantages-of-web-workers">Advantages of Web workers</h3>
<p>Web workers handle pure data, which makes them especially suitable for JavaScript code that takes a long time to execute.</p>
<p><em>To summarize:</em></p>
<ul>
<li>Web workers operate independently of the main browser UI thread.</li>
<li>Ideal for running computationally expensive tasks in background threads without affecting the UI thread.</li>
<li>Parallel processing for better application performance.</li>
<li>Web workers live in a restricted and thread-safe environment.</li>
</ul>
<h3 id="browser-support">Browser Support</h3>
<p>Basic web worker support is good across all browsers but I do not recommend using shared web workers as the support is very limited.</p>
<p><em>Basic web worker support:</em></p>
<ul>
<li>Safari Mobile 5.1</li>
<li>Android 4.4</li>
<li>Chrome 4</li>
<li>IE 10</li>
</ul>
<p>You can check upto date support at <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers">mozilla.org</a> and <a href="http://caniuse.com/#search=web%20worker">caniuse.com</a>.</p>
<h3 id="limitations">Limitations</h3>
<p>Worker thread <strong>DO NOT</strong> have access to:</p>
<ul>
<li>DOM</li>
<li>Window object</li>
<li>Document object</li>
<li>Parent object</li>
<li>Global variables or functions in main page</li>
</ul>
<p>Also note that all of your worker scripts must be served from the same domain and protocol as the script that is attempting to create the worker.</p>
<p>###Ideal Candidates/Use cases</p>
<p>The most important thing is to identify and isolate hot spots in your code so that they can me moved to web workers for parallel execution provided they do not have any limitations mentioned above.</p>
<h3 id="example">Example</h3>
<p>The following is a sample web worker which loads and parses JSON file.</p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">package webworkers;
import haxe.Json;
import js.html.XMLHttpRequest;
class LoadJson {
var _data:Dynamic;
public static function __init__() {
untyped __js__("self.onmessage = webworkers_LoadJson.prototype._messageHandler");
}
function _messageHandler(event) {
switch(event.data) {
case "load":
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState == 4) {
if (request.status == 200) {
_data = Json.parse(request.responseText);
untyped __js__("self").postMessage("LOADED");
}
}
}
request.open("GET", "../resources/checkstyle-config.json", true);
request.send(null);
default:
for (i in 0 ... _data.checks.length) {
if (_data.checks[i].type == event.data) {
untyped __js__("self").postMessage(_data.checks[i]);
break;
}
}
}
}
}</code></pre></figure>
<p>Add the following to your <strong>build.hxml</strong> to generate worker script file:</p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">webworkers.LoadJson
-js js/LoadJson.js</code></pre></figure>
<p>The following sample shows how to load and communicate with the worker script.</p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">package webworkers;
import js.html.Worker;
class Main {
var _worker:Worker;
public function new() {
_worker = new js.html.Worker("js/LoadJson.js");
_worker.onmessage = _processWorkerJsonData;
_worker.postMessage("load");
}
function _processWorkerJsonData(e) {
switch (e.data) {
case "LOADED":
_worker.postMessage("AccessOrder");
default:
trace(e.data);
}
}
static function main() {
new Main();
}
}</code></pre></figure>
<p>Note that web workers are not automatically garbage collected and will live until you manually terminate them.</p>
<p><code class="highlighter-rouge">_worker.terminate();</code></p>
<p>You can also terminate the worker by calling <code class="highlighter-rouge">close()</code> method on itself.</p>
<h3 id="references">References</h3>
<ul>
<li><a href="http://www.w3.org/TR/workers/">w3.org</a></li>
<li><a href="http://www.w3schools.com/html/html5_webworkers.asp">w3schools</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers">mozilla.org</a></li>
</ul>
<p>You can check my <a href="https://github.com/adireddy/playground/tree/master/src/webworkers">repo</a> for more samples.</p>
<p>Any questions or comments, please post them in the comments section below. Thanks for reading.</p>
<p><a href="/haxe/web/workers/multi-threading/javascript/web-workers">Web Workers</a> was originally published by Adi Reddy Mora at <a href="">Adi Reddy Mora</a> on November 17, 2015.</p>/haxe/getters/setters/haxe-getters-and-setters2015-01-17T00:00:00+00:002015-01-17T00:00:00-00:00Adi Reddy Moraadiinteractive@gmail.com
<p>Haxe has a unique way of using getter/setter functions and it can be sometimes confusing for developers coming from other languages.</p>
<p>I recommend to read the official <a href="http://haxe.org/manual/class-field-property-rules.html">documentation</a> as well.</p>
<hr />
<p><strong>Getter and Setter</strong></p>
<p>You can define a variable to have getter and setter functions using <code class="highlighter-rouge">get</code> and <code class="highlighter-rouge">set</code> keywords as shown below.</p>
<p>The functions names should be prefixed with <code class="highlighter-rouge">get_</code> variable name and <code class="highlighter-rouge">set_</code> variable name.</p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">public var amount(get, set):Float;
var _amount:Float = 100;
function get_amount():Float {
return _amount * 10;
}
function set_amount(value:Float) {
return _amount = value;
}</code></pre></figure>
<p>The most important thing to note here is you cannot access the physical variable inside your getter and setter functions. You get the error <code class="highlighter-rouge">This field cannot be accessed because it is not a real variable</code> if you try to access <code class="highlighter-rouge">amount</code> inside <code class="highlighter-rouge">get_amount</code> and <code class="highlighter-rouge">set_amount</code> functions.</p>
<p>To access physical variable you need to add metadata <code class="highlighter-rouge">@:isVar</code> in front of the variable <code class="highlighter-rouge">amount</code>.</p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">@:isVar public var amount(get, set):Float;
function get_amount():Float {
return amount;
}
function set_amount(value:Float) {
return amount = value;
}</code></pre></figure>
<hr />
<p><strong>Getter (read-only) or Setter (write-only)</strong></p>
<p>By using <code class="highlighter-rouge">null</code> in place of <code class="highlighter-rouge">set</code> you can mark the variable read-only with getter function as shown below.</p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">public var language(get, null):String;
function get_language():String {
return language;
}</code></pre></figure>
<p>Similarly by using <code class="highlighter-rouge">null</code> in place of <code class="highlighter-rouge">get</code> you can mark the variable write-only with setter function as shown below.</p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">public var language(null, set):String;
function set_language(value:String):String {
return language = value;
}</code></pre></figure>
<p>The variable can still be accessed for reading and writing within the class but not from outside.</p>
<hr />
<p><strong>Setter with default Getter</strong></p>
<p>By using <code class="highlighter-rouge">default</code> in place of <code class="highlighter-rouge">get</code> you can mark the variable to return it’s value when accessed and still have a setter function.</p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">public var language(default, set):String;
function set_language(value):String {
return language = value;
}</code></pre></figure>
<hr />
<p><strong>Quick Reference</strong></p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">//Read and write access from anywhere but only exception is the physical variable cannot be accessed within getter and setter functions without metadata @:isVar
public var x(get, set):Float;
//Can be read from anywhere, modified only within the class
public var x(default, null):Float;
//Can be set from anywhere, modified only within the class
public var x(null, default):Float;
//Read-only from anywhere even within the same class
public var x(get, never):Float;
//Read and write access from anywhere, but the setter needs a method
public var x(default, set):Float;
//not allowed - unsupported property combination
public var x(get, default):Float;</code></pre></figure>
<p><a href="/haxe/getters/setters/haxe-getters-and-setters">Haxe Getters and Setters</a> was originally published by Adi Reddy Mora at <a href="">Adi Reddy Mora</a> on January 17, 2015.</p>/haxe/compiler/metadata/haxe-compiler-metadata2015-01-10T00:00:00+00:002015-01-10T00:00:00-00:00Adi Reddy Moraadiinteractive@gmail.com
<p>Haxe compiler metadata can be really handy to get specific behavior or to tweak the language based on your needs.</p>
<p>You can get the full list of supported compiler flags by running <code class="highlighter-rouge">haxe --help-metas</code>.</p>
<p>It’s a big list and the following are some of my favourites.</p>
<hr />
<p><code class="highlighter-rouge">@:overload</code> - I use this a lot when writing externs where you can overload fucntion paramater declarations and return type. The first one that is matched will be used.</p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">@:overload(function(shape:Rectangle):GraphicsData {})
@:overload(function(shape:Ellipse):GraphicsData {})
@:overload(function(shape:Polygon):GraphicsData {})
function drawShape(shape:Circle):GraphicsData;</code></pre></figure>
<hr />
<p><code class="highlighter-rouge">@:final</code> - When you want to mark the class as final.</p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">@:final
class MyClass {}</code></pre></figure>
<p>If you try to extend <code class="highlighter-rouge">MyClass</code> you will get compile time error <code class="highlighter-rouge">Cannot extend a final class</code>.</p>
<hr />
<p><code class="highlighter-rouge">@:optional</code> - When you want to declare optional fields in <code class="highlighter-rouge">typedef</code>.</p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">typedef TextField = {
var text:String;
@:optional var font:String;
}</code></pre></figure>
<hr />
<p><code class="highlighter-rouge">@:publicFields</code> - When you want to change the default visibility of the whole class and it’s sub classes from <code class="highlighter-rouge">private</code> to <code class="highlighter-rouge">public</code>. I find this useful for static classes.</p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">@:publicFields
class MyClass {
static inline var width:Float = 1024;
static inline var height:Float = 768;
static inline var ratio:Float = 1;
}</code></pre></figure>
<hr />
<p><code class="highlighter-rouge">@:keep</code> - Unused variables and functions will be removed with dead code elimination <code class="highlighter-rouge">-dce full</code>. You can force to keep them using this metadata.</p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">@:keep public function getChildByName(name:String):DisplayObject {
return _container.getChildByName(name);
}</code></pre></figure>
<hr />
<p>All of the above are generic metadata and can be used across platforms and there are many more generic and platform specific metadata you may find useful for your specific needs so explore and please post in the comments section if you find anything interesting.</p>
<p><strong>Update (14 Jan 2015)</strong>: Another JavaScript specific metadata which I find useful is <code class="highlighter-rouge">@:expose</code>. It can be used on any class to make it available/accessible from the window object. This is useful when you want to expose any of your haxe classes to another library via window object.</p>
<p><a href="/haxe/compiler/metadata/haxe-compiler-metadata">Haxe Compiler Metadata</a> was originally published by Adi Reddy Mora at <a href="">Adi Reddy Mora</a> on January 10, 2015.</p>/haxe/documentation/haxe-documentation generators2014-12-10T00:00:00+00:002014-12-10T00:00:00-00:00Adi Reddy Moraadiinteractive@gmail.com
<p>A quick intro to a couple of documentation generators I have tried recently <a href="https://github.com/ibilon/chxdoc">chxdoc</a> and <a href="http://yui.github.io/yuidoc/">yuidoc</a>.</p>
<p>Note that yuidoc is Javascript Documentation Tool which also works with Haxe.</p>
<h3 id="chxdoc">chxdoc</h3>
<p>Make sure you have chxdoc installed.</p>
<p><code class="highlighter-rouge">haxelib install chxdoc</code></p>
<p>chxdoc generates Java like documentation based on the XML output generated by haxe compiler using <code class="highlighter-rouge">xml</code> compiler parameter as shown below.</p>
<p><code class="highlighter-rouge">haxe -xml docs.xml --macro include('pixi')</code></p>
<p>By default, compiler generates the xml documentation for haxe package and you can use macros to include custom packages like <code class="highlighter-rouge">pixi</code> in the above sample.</p>
<p>When you have the xml documentation ready you can run <a href="https://github.com/ibilon/chxdoc">chxdoc</a> command and the following is the one I used to generate <a href="http://adireddy.github.io/docs/haxe-pixi/">pixi.js haxe externs documentation</a>.</p>
<p><code class="highlighter-rouge">haxelib run chxdoc -o output_folder -f docs.xml --deny=haxe.*,/ --title='Externs of pixi.js for Haxe' --subtitle='<a href='http://adireddy.github.io/haxe-pixi' target='_blank'>haxe-pixi</a>'</code></p>
<ul>
<li>output_folder - The folder path where you want the documentation to be generated</li>
<li>–deny - You can specify the <code class="highlighter-rouge">,</code> separated list of all the packages you want to exclude from the documentation (haxe package and <code class="highlighter-rouge">/</code> denotes root classes in the above sample).</li>
</ul>
<p>I hope all the other options are self explanatory.</p>
<p>You can also use <code class="highlighter-rouge">--developer=true</code> to generate developer documentation with all the private data documented.</p>
<h3 id="yuidoc">YUIDoc</h3>
<p>YUIDoc is much more simpler to use and I only tried it because <a href="http://www.goodboydigital.com/pixijs/docs/">pixi.js</a> is using it for their documentation.</p>
<p>It’s is Node.js application and you should install it using node package manager <code class="highlighter-rouge">npm -g install yuidocjs</code></p>
<p>I have created <code class="highlighter-rouge">yuidoc.json</code> file to store the configuration.</p>
<pre><code class="language-haxe">{
"name": "haxe-pixi",
"description": "Haxe Pixi Externs",
"version": "2.1.9",
"url": "https://github.com/adireddy/haxe-pixi",
"options": {
"linkNatives": "true",
"attributesEmit": "true",
"selleck": "true",
"ignorePaths": [ "demos" ],
"paths": "*/hx",
"outdir": "yui"
}
}
</code></pre>
<p>Finally run the following command to generate the documentation.</p>
<p><code class="highlighter-rouge">yuidoc -e .hx .</code></p>
<p>Here is the link to <a href="http://adireddy.github.io/docs/haxe-pixi/yui/">haxe pixi externs</a> YUIDoc documentation.</p>
<p>Any comments/queries, please post them in the comments section.</p>
<p><a href="/haxe/documentation/haxe-documentation-generators">Haxe Documentation Generators</a> was originally published by Adi Reddy Mora at <a href="">Adi Reddy Mora</a> on December 10, 2014.</p>/haxe/nape/pixi.js/haxe-pixi-nape-demo2014-12-05T00:00:00+00:002014-12-05T00:00:00-00:00Adi Reddy Moraadiinteractive@gmail.com
<p>A quick basic physics demo using haxe, pixi.js and nape just to make sure the combination works and well it works :)</p>
<p><a href="http://adireddy.github.io/demos/haxe-pixi/nape.html">Click here for the demo</a> and source code below.</p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">package samples.nape;
import pixi.renderers.IRenderer;
import pixi.display.Sprite;
import pixi.display.Stage;
import pixi.textures.Texture;
import pixi.utils.Detector;
import js.Browser;
import haxe.Timer;
import nape.geom.Vec2;
import nape.phys.Body;
import nape.phys.BodyType;
import nape.shape.Circle;
import nape.shape.Polygon;
import nape.space.Space;
import nape.phys.Material;
class Main {
var _renderer:IRenderer;
var _stage:Stage;
var _floor:Body;
var _space:Space;
var _balls:Array<Sprite>;
var _pballs:Array<Body>;
public function new() {
_stage = new Stage(0x00FFFF);
_renderer = Detector.autoDetectRenderer(800, 600);
Browser.document.body.appendChild(_renderer.view);
_balls = [];
_pballs = [];
_setUpPhysics();
var timer:Timer = new Timer(1000);
timer.run = _addBall;
Browser.window.requestAnimationFrame(cast animate);
}
function _setUpPhysics() {
var gravity = Vec2.weak(0, 600);
_space = new Space(gravity);
_floor = new Body(BodyType.STATIC);
_floor.setShapeMaterials(Material.wood());
_floor.shapes.add(new Polygon(Polygon.rect(0, 595, 800, 1)));
_floor.space = _space;
}
function _addBall() {
var ball:Sprite = new Sprite(Texture.fromImage("assets/nape/ball.png"));
ball.anchor.set(0.5, 0.5);
_balls.push(ball);
_stage.addChild(ball);
var pball:Body = new Body(BodyType.DYNAMIC);
pball.shapes.add(new Circle(10));
pball.position.setxy(Std.random(800), 0);
pball.angularVel = 0;
pball.allowRotation = true;
pball.setShapeMaterials(Material.rubber());
pball.space = _space;
_pballs.push(pball);
}
function animate() {
Browser.window.requestAnimationFrame(cast animate);
_space.step(1 / 60);
for(i in 0 ... _pballs.length) {
_balls[i].position.x = _pballs[i].position.x;
_balls[i].position.y = _pballs[i].position.y;
_balls[i].rotation = _pballs[i].rotation;
}
_renderer.render(_stage);
}
static function main() {
new Main();
}
}</code></pre></figure>
<p><a href="/haxe/nape/pixi.js/haxe-pixi-nape-demo">Haxe Nape & Pixi.js Demo</a> was originally published by Adi Reddy Mora at <a href="">Adi Reddy Mora</a> on December 05, 2014.</p>/haxe/api/documentation/chxdoc/haxe-pixi-externs-api2014-12-04T00:00:00+00:002014-12-04T00:00:00-00:00Adi Reddy Moraadiinteractive@gmail.com
<p>I was playing with documentation generator library <a href="https://github.com/ibilon/chxdoc">chxdoc</a> this week and amazingly it covered all the basics I needed for generating API documentation.</p>
<p>With a bit of template customization, I have generated API documentation for my <a href="http://adireddy.github.io/docs/haxe-pixi/">pixi.js haxe externs</a> and it looked pretty good.</p>
<p>Even though most of the contents of this API are same as <a href="http://www.goodboydigital.com/pixijs/docs/">pixi.js</a> API, I think it is a good reference to check the haxe equivalent data types of variables, function parameters and return types. You can also use this to quickly check if any of the functionality is missing in the extern classes.</p>
<p>I will explain more on how to use chxdoc library in my next post.</p>
<p><a href="/haxe/api/documentation/chxdoc/haxe-pixi-externs-api">Haxe pixi.js externs API (using chxdoc)</a> was originally published by Adi Reddy Mora at <a href="">Adi Reddy Mora</a> on December 04, 2014.</p>/haxe/static/extensions/using/haxe-static-extensions2014-11-27T00:00:00+00:002014-11-27T00:00:00-00:00Adi Reddy Moraadiinteractive@gmail.com
<p>Static extensions in Haxe can be used to mimic multiple inheritance similar to mixins in Javascript. It’s a powerful feature and should be used carefully.</p>
<p><a href="http://api.haxe.org/StringTools.html">StringTools</a> class is a good example of station extension. Refer to my <a href="http://adireddy.github.io/2014/11/17/haxe-string-interpolation-stringutils/">earlier post</a> for more info on that.</p>
<p><strong>Quick Example:</strong></p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">package ;
class ArrayUtils {
public static inline function last<T>(a:Array<T>):T {
return a[a.length - 1];
}
}</code></pre></figure>
<p><code class="highlighter-rouge">using</code> keyword brings the defined class into the context.</p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">package ;
using ArrayUtils;
class Example {
public function new() {
var arr:Array<Int> = [100, 200, 500];
trace(arr.last()); //500
}
}</code></pre></figure>
<p><strong>Another example with multiple static extensions:</strong></p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">package ;
class ArrayUtils2 {
public static inline function first<T>(a:Array<T>):T {
return a[0];
}
}</code></pre></figure>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">package ;
using ArrayUtils;
using ArrayUtils2;
class Example {
public function new() {
var arr:Array<Int> = [100, 200, 500];
trace(arr.last()); //500
trace(arr.first()); //100
}
}</code></pre></figure>
<p>A few things to note:</p>
<ul>
<li>If the same method is defined in multiple static extension classes, <code class="highlighter-rouge">using</code> expressions are checked from bottom to top. For example if the method <code class="highlighter-rouge">last</code> is also defined in <code class="highlighter-rouge">ArrayUtils2</code> then it will take precedence as <code class="highlighter-rouge">ArrayUtils2</code> is the bottom class in the above example.</li>
<li>Built in class fields would take priority over the static extension fields. For example if you define <code class="highlighter-rouge">toString</code> method in <code class="highlighter-rouge">ArrayUtils</code> class above it wouldn’t work as <code class="highlighter-rouge">toString</code> is a built in array method and it takes precedence.</li>
<li>Compiler metadata <code class="highlighter-rouge">@:noUsing</code> can be used in static extension class if you want to omit any field being included in the context. Check the example below.</li>
</ul>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">package ;
class ArrayUtils {
public static inline function last<T>(a:Array<T>):T {
return a[a.length - 1];
}
@:noUsing
public static inline function second<T>(a:Array<T>):T {
return a[1];
}
}</code></pre></figure>
<p>You can’t use the method <code class="highlighter-rouge">second</code> as the compiler will not include it in the context when used.</p>
<p><a href="/haxe/static/extensions/using/haxe-static-extensions">Haxe Static Extensions</a> was originally published by Adi Reddy Mora at <a href="">Adi Reddy Mora</a> on November 27, 2014.</p>/haxe/haxe-access-modifiers-return-types2014-11-20T00:00:00+00:002014-12-10T00:00:00-00:00Adi Reddy Moraadiinteractive@gmail.com
<p>Any developers out there coming from ActionScript or Java background like me, the default access modifiers and function return type in Haxe are a bit different. So let’s have a quick look at them in classes, interfaces and externs.</p>
<h2 id="classes">Classes:</h2>
<ul>
<li>Default access modifier is <code class="highlighter-rouge">private</code> if none specified.</li>
<li>Default function return value type is <code class="highlighter-rouge">Void</code> if there is no return expression or if the return expression has no argument.</li>
</ul>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">package ;
class Test {
//private variable
var _count:Int;
//constructor
public function new() {
}
//private function
//no return expression, so return value type is Void
function _increaseCount() {
_count++;
}
//private function
//return expression has no argument, so return value type is Void
function _resetCount() {
if (_count < 10) return;
_count = 0;
}
}</code></pre></figure>
<h2 id="interfaces-and-externs">Interfaces and Externs:</h2>
<ul>
<li>Default access modifier is <code class="highlighter-rouge">public</code>.</li>
<li>Specifying return value type is compulsory for functions. Failing so will result in compile time error <code class="highlighter-rouge">Type required for extern classes and interfaces</code></li>
</ul>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">package ;
interface ITest {
//public variable
var count:Int;
//public function
function increaseCount():Void;
}</code></pre></figure>
<p>When implementing the above interface you need to specify <code class="highlighter-rouge">public</code> access modifier to variable <code class="highlighter-rouge">count</code> and function <code class="highlighter-rouge">increaseCount</code> as shown below. Failing so will result in the following compile time errors.</p>
<ul>
<li><code class="highlighter-rouge">Field count should be public as requested by ITest</code></li>
<li><code class="highlighter-rouge">Field increaseCount should be public as requested by ITest</code>.</li>
</ul>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">package ;
class Test implements ITest {
public var count:Int = 10;
public function increaseCount():Void {
count++;
}
}</code></pre></figure>
<p>Interesting fact from the official documentation about <code class="highlighter-rouge">protected</code>:</p>
<blockquote>
<p>Haxe has no <code class="highlighter-rouge">protected</code> keyword like in ActionScript, Java, etc. However, its <code class="highlighter-rouge">private</code> behavior is equal to those language’s <code class="highlighter-rouge">protected</code> behavior, so in reality Haxe lacks in <code class="highlighter-rouge">private</code> behavior.</p>
</blockquote>
<p><a href="/haxe/haxe-access-modifiers-return-types">Haxe default Access Modifiers and Return Value Type</a> was originally published by Adi Reddy Mora at <a href="">Adi Reddy Mora</a> on November 20, 2014.</p>/haxe/string/interpolation/stringtools/haxe-string-interpolation-stringutils2014-11-17T00:00:00+00:002014-11-17T00:00:00-00:00Adi Reddy Moraadiinteractive@gmail.com
<p>String interpolation is common in many programming languages including modern languages like <a href="https://www.dartlang.org/articles/idiomatic-dart/#strings-and-interpolation">Dart</a>, <a href="https://developer.apple.com/library/ios/documentation/swift/conceptual/Swift_Programming_Language/StringsAndCharacters.html">Swift</a>, etc and I am glad that it is also available in Haxe.</p>
<p>String interpolation provides an intuitive way of formatting strings in place of string concatenation. It is a compile-time feature and has no impact on the runtime in Haxe.</p>
<p><code class="highlighter-rouge">$</code> sign should be used for placeholders and string should be enclosed in single-quote <code class="highlighter-rouge">'</code> character as shown in the example below.</p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">var winnings:Float = 10;
var bonus:Float = 2;
trace('Player won £$winnings'); //Player won £10
trace('Player won £${winnings + bonus} including bonus');
//Player won £12 including bonus</code></pre></figure>
<p>Next, <strong>StringTools</strong> is another useful class for advanced string manipulation and formatting. It’s kept separate to keep the core String class light and this class can be used based on the application needs.</p>
<p>StringTools should be used as a static extension to the String class with <code class="highlighter-rouge">using</code> keyword as shown below.</p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">using StringTools;
class Main {
static public function main() {
var str = "apples and bananas";
var newStr = str.replace("bananas", "grapes");
trace(newStr); //apples and grapes
trace(newStr.endsWith("grapes")); //true
}
}</code></pre></figure>
<p>Definition of Static Extension as per the official documentation:</p>
<blockquote>
<p>A static extension allows pseudo-extending existing types without modifying their source. In Haxe this is achieved by declaring a static method with a first argument of the extending type and then bringing the defining class into context through <code class="highlighter-rouge">using</code>.</p>
</blockquote>
<p><a href="/haxe/string/interpolation/stringtools/haxe-string-interpolation-stringutils">Haxe String Interpolation and StringTools</a> was originally published by Adi Reddy Mora at <a href="">Adi Reddy Mora</a> on November 17, 2014.</p>/haxe/work/haxe-at-work2014-11-12T00:00:00+00:002014-11-12T00:00:00-00:00Adi Reddy Moraadiinteractive@gmail.com
<p>Over the last 2-3 years I have been exploring Haxe and OpenFL personally but now I am on cloud nine because I can officially use Haxe at work.</p>
<p>I should thank my company <a href="http://www.gamesyscorporate.com">Gamesys</a> for giving the opportunity to showcase the power of Haxe and how it can benefit the company and the developers.</p>
<p>I managed to convince the team and got them excited as well, so looking forward to develop cool stuff using Haxe :)</p>
<blockquote>
<p>“The idea of waiting for something makes it more exciting” ― Andy Warhol</p>
</blockquote>
<p><a href="/haxe/work/haxe-at-work">Haxe at Work</a> was originally published by Adi Reddy Mora at <a href="">Adi Reddy Mora</a> on November 12, 2014.</p>/haxe/javascript/externs/why-haxe2014-11-11T00:00:00+00:002014-11-11T00:00:00-00:00Adi Reddy Moraadiinteractive@gmail.com
<p>Haxe is simple, lightweight, easy to learn, yet a very powerful strictly typed language with lightning fast compiler.</p>
<p>If your main target is JavaScript like me, Haxe can be a better coding option than coding in JavaScript directly. Haxe will transpile your code to clean and efficient JavaScript.</p>
<p>There are a lot of useful libraries readily available at <a href="http://lib.haxe.org">lib.haxe.org</a>.</p>
<p>I have created haxe externs for some popular JavaScript libraries and there are few libraries available from others listed below together.</p>
<ul>
<li><a href="http://adireddy.github.io/haxe-pixi">haxe-pixi</a></li>
<li><a href="http://adireddy.github.io/haxe-howler">haxe-howler</a></li>
<li><a href="http://adireddy.github.io/haxe-buzz">haxe-buzz</a></li>
<li><a href="https://github.com/adireddy/haxe-p2">haxe-p2</a></li>
<li><a href="https://github.com/luizbills/stats.js.hx">stats.js.hx</a></li>
<li><a href="https://github.com/Blank101/haxe-phaser">haxe-phaser</a></li>
<li><a href="https://github.com/adireddy/haxe-threejs">haxe-threejs</a></li>
</ul>
<p><a href="/haxe/javascript/externs/why-haxe">Why Haxe?</a> was originally published by Adi Reddy Mora at <a href="">Adi Reddy Mora</a> on November 11, 2014.</p>/haxe/keywords/never-inline-keywords2014-11-08T00:00:00+00:002014-11-08T00:00:00-00:00Adi Reddy Moraadiinteractive@gmail.com
<p>There is no <code class="highlighter-rouge">const</code> keyword in Haxe to define constants like in ActionScript.</p>
<p>So <code class="highlighter-rouge">never</code> keyword can be used to define constants/read-only variables as shown below.</p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">public var MAX_COUNT(default, never):Int = 100;</code></pre></figure>
<p>If you try to set the value of MAX_COUNT, compiler will throw an error saying “<em>Cannot access field or identifier MAX_COUNT for writing</em>”.</p>
<p>For static variables and functions <code class="highlighter-rouge">inline</code> keyword can be used as a powerful optimization tool.</p>
<p>The following is a way to define static constant/read-only variables using “inline”.</p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">public static inline var MAX_COUNT:Int = 100;</code></pre></figure>
<p><code class="highlighter-rouge">inline</code> should be carefully used with functions as not all functions will be ideal candidates for optimization.</p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">public static inline function stageCenter(sw:Float, iw:Float):Float {
return (sw - iw) / 2;
}</code></pre></figure>
<p>At run-time the function call</p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">var pos = stageCenter(800, 100);</code></pre></figure>
<p>will be translated to the following by eliminating the function call.</p>
<figure class="highlight"><pre><code class="language-haxe" data-lang="haxe">var pos = (800 - 100) / 2;</code></pre></figure>
<p>As per the official documentation,</p>
<blockquote>
<p>It is not always easy to judge if a function is ideal for inline. Short functions that have no writing expressions (such as <code class="highlighter-rouge">a = value</code>) are usually a good choice, but even more complex functions can be candidates. However, in some cases inlining can actually be detrimental to performance, e.g. because the compiler has to create temporary variables for complex expressions.</p>
</blockquote>
<p>Note that <code class="highlighter-rouge">inline</code> can be totally disabled by passing compiler argument <em><code class="highlighter-rouge">--no-inline</code></em>.</p>
<p><a href="/haxe/keywords/never-inline-keywords">Haxe "never" and "inline" keywords</a> was originally published by Adi Reddy Mora at <a href="">Adi Reddy Mora</a> on November 08, 2014.</p>/haxe/openfl/flash%20cc/openfl-in-flash-cc2014-11-02T00:00:00+00:002014-11-02T00:00:00-00:00Adi Reddy Moraadiinteractive@gmail.com
<p>With the new “<a href="http://www.adobe.com/uk/products/flash/features.html">Custom platform support</a>” in Flash Professional CC, Haxe/OpenFL will soon be available in Flash IDE which is a great news for Haxe developers.</p>
<p>Look at Joshua Granick’s <a href="http://max.adobe.com/sessions/max-online/#/video/567">quick demo</a> showing how it works.</p>
<p>You can skip to Haxe/OpenFL demo which starts at around 25:00.</p>
<p><a href="/haxe/openfl/flash%20cc/openfl-in-flash-cc">Haxe/OpenFL will soon be integrated with Flash CC IDE</a> was originally published by Adi Reddy Mora at <a href="">Adi Reddy Mora</a> on November 02, 2014.</p>/haxe/javascript/benefits-of-transpiling-to-js2014-10-31T00:00:00+00:002014-10-31T00:00:00-00:00Adi Reddy Moraadiinteractive@gmail.com
<p>A beautiful article about “<a href="http://io.pellucid.com/blog/the-benefits-of-transpiling-to-javascript">The benefits of transpiling to JavaScript</a>” by Franco Ponticelli.</p>
<blockquote>
<p>A transpiler is a type of compiler that takes the source code of a programming language as its input and outputs the source code of another programming language.</p>
</blockquote>
<p><a href="/haxe/javascript/benefits-of-transpiling-to-js">The Benefits of Transpiling to JavaScript</a> was originally published by Adi Reddy Mora at <a href="">Adi Reddy Mora</a> on October 31, 2014.</p>