Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
haXe 2 Beginner's Guide

You're reading from  haXe 2 Beginner's Guide

Product type Book
Published in Jul 2011
Publisher
ISBN-13 9781849512565
Pages 288 pages
Edition 1st Edition
Languages

Table of Contents (21) Chapters

haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Getting to know haXe 2. Basic Syntax and Branching 3. Being Cross-platform with haXe 4. Understanding Types 5. The Dynamic Type and Properties 6. Using and Writing Interfaces, Typedefs, and Enums 7. Communication Between haXe Programs 8. Accessing Databases 9. Templating 10. Interfacing with the Target Platform 11. A Dynamic Website Using JavaScript 12. Creating a Game with haXe and Flash Pop Quiz Answers Index

Chapter 10. Interfacing with the Target Platform

Working with your platform.

Being able to use the interns of the platform you are targeting or to work with native code can be really useful: it may lead to being able to easily use some functionality of a library or to optimize your code. However, it also has its drawbacks.

We have now learned about the basics of haXe and are going to get into more technical stuff—interfacing with the target platform. Doing so may be really useful for several things, such as using a library that has been written for the target platform. It is sometimes useful in order to optimize your code too.

In this chapter we will:

  • Learn about what extern classes are

  • The magic functions

  • Interfacing with native code

So, if you are ready, let's go on!

Extern classes


Extern classes allow one to tell the compiler what classes exist outside of your own code. This way, the compiler won't tell you that the class does not exist and it will also know what fields exist in it and what their type is.

Time for action – Writing an extern


Imagine that we are writing a User class in JavaScript:

function User()
{
   var name;
   var age;
}
User.prototype.outputInfo = function()
{
   var el = document.createElement("div");
   el.innerHTML = this.name+"("+ this.age+")";
   document.body.appendChild(el);
}

Now, if we want to use this class from our haXe application, then we have to write the corresponding extern class as follows:

extern class User
{
   public var name:String;
   public var age:Int;
   public function outputInfo():Void;
   public function new():Void;
}

There are several things that you should note:

  • You have to prefix your class declaration with the extern keyword

  • We do not write any code inside function declaration

  • Constructors should be declared returning Void

What just happened?

We have written an extern class, explaining to the compiler that the User class exists outside our code and can be used from it.

Using an extern

Now, we can use this class in our haXe code:

class Main
{   ...

Native types and haXe types


haXe defines some types that may already exist in the target platform.

For example, haXe defines the Array class. The Array class is already defined on almost all target platforms. The thing is that on some targets, the haXe array and the native array may indeed be different.

In such cases, when working with externs, you may need to convert from a haXe array to a native array and vice-versa.

You have to pay particular attention to that when working with Neko and PHP.

The Neko and PHP cases

If you are targeting Neko or PHP, then you will have to pay particular attention to array and string. Indeed, in the Neko and PHP packages, you will find the native array and native string types.

There are also 'functions and methods that you can use to convert a native type to the haXe one.

PHP functions

The following are the functions that you can use in PHP to convert from native types to haXe types:

Magic functions


Magic functions are special functions that should be handled with care—they are very useful to interface with your target platform, but they can produce undefined behavior if they are not correctly used. In addition, they do not provide any kind of typing as they can only be used inside an untyped block.

Available magic functions are different from one platform to another and therefore, if you are targeting several platforms, you really have to be careful and should consider using conditional compilation.

The Flash magic

The Flash platform is certainly the one with the most magic functions. Certainly, you will not use all of them straight away, but they can always be useful. So, let's go on with them.

In addition, some of them are only available when targeting a specific version of Flash.

__new__

Note

Available in Flash 6 to 10

The __new__ magic function allows you to create an instance of a class. Its syntax is as follows:

__new__(cl : Dynamic, param1 : Dynamic, param2 : Dynamic...

Summary


In this chapter, we've learned how you can interface your haXe code with your target platform by using magic functions and using the __init__ magic. We have also seen how to use several magic functions.

Now that we've seen how to interface haXe code with the target platform, we are going to create a completely dynamic website with haXe targeting Javascript.

lock icon The rest of the chapter is locked
You have been reading a chapter from
haXe 2 Beginner's Guide
Published in: Jul 2011 Publisher: ISBN-13: 9781849512565
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at ₹800/month. Cancel anytime}

Functions

Explanation

php.Lib.associativeArrayOfHash

This function creates...