Halaman

Jumat, 25 Juli 2014

[Haxe] Operator overloading!

yep, Haxe is powerful, but every language has their own weaknesses and strengths. To do a simple class overloading, we need to declare compilers meta and using 'abstracts' which are not quite comfy.. but for the sake of knowledge.. here is a snippet to implement a simple operator overloading in haxe..

@:allow(Mimit)
class _MyBase {
 var v: Int;
 public var y: Int;
 
 public function new(v: Int)
 {
  this.v = v;
  y = v * v;
 }
}

class _Mimit extends _MyBase{
 public function test() {
  trace(v);
 }
}

@:forward
abstract Mimit(_Mimit) from _Mimit to _Mimit {
 public function new(v:Int) this = new _Mimit(v);
 
 @:op(A + B) public function add(rhs:Mimit):Mimit {
  var t: Mimit = new Mimit(this.v + rhs.v);
  return t;
 }
}
let's analyze here for a momment...

  1. The class _MyBase needs a syntatix sugar of operator overloading, so that user could simply call _MyBase * _MyBase and use them accordingly
  2. Since the operator need to access 'private' member of the class we add the compiler's meta @:allow(Mimit) since the abstract Mimit will provided the overloads..
  3. And since the programmer will directly use the abstract class "Mimit" we declare another meta @:forward to the declaration, and telling the abstract to "implicitly" support _Mimit class on usages(fields and methods) by writing from _Mimit to _Mimit
  4. The from _Mimit to _Mimit basicly tells the compiler to please implicitly assign the this variable to _mimit and assign the this variable from _Mimit
There we have it, an operator overloading which works on haxe..
For the Usage: 
var m1: Mimit = new Mimit(10);
var m2: Mimit = new Mimit(20);
var m3: Mimit = m1 + m2;
  
m3.test();
trace(m3.y);
Frankly i'm not quite satisfied with things done this way.. a simple operator overloading made a bloated code base, i think i will stick to the static function..Cheers! :)

Rabu, 23 Juli 2014

[OpenFL/Haxe/HXCPP] Debugging

Playing with OpenFL is fun! mainly because of it's power for cross platform development.
Debugging program which target the flash platform is very easy, you just need the FlashDevelop software and download the flash player with the debug support

a step by step procedure to debug the "FLASH" target
  1. Open the project
  2. "Menu" »"Debug" »"Start Remote Session"
  3. Make sure you are using the "debug" configuration, and then press the "Play / Run" button
  4. Set's your breakpoints, inspect variables, etc is a snap..
If we are debugging windows target (hxcpp) this will introduce different approach, FlashDevelop currently working to support it 

But for those who like to use the command prompt, here is a step by step procedure..
  1. run "haxelib install hxcpp-debugger"
  2. on your project's application.xml add
    <haxedef name="HXCPP_DEBUGGER" />
    <haxelib name="hxcpp-debugger" />
  3. on your MAIN class file, add the import: "import debugger.Local;"
  4. on main haxe file, you need to add the function call
    new Local(true);
  5. run the program on your command prompt:
    lime test window
  6. It will run and then hit a default breakpoint, from there you should type "help" and read the documentation to proceed
  7. nice........
Haxe is quite mature, but it's cross platform nature made the debugging process which should be simple, becomes quite a challenging process, OpenFL supports html5 target, next i will try to learn on mapping js to haxe's source code using the technology source-code mappings on chrome and firefox browser..