Haxe: East Travels (Upper) Part1:intro

Source: Internet
Author: User

Original title: A tentative study on the haxe of ramblings in West Line

1. Description: What is Haxe?

Haxe (read as a clam Cornu) is an open-source cross-platform solution that was created in the previous Flash era. Learn to Haxe, you can directly use this kind of typescript syntax, generate corresponding js/php/python/c++ and other source code, can also be directly compiled into a script to specify target (such as EXE

1.1 Historical background

Haxe forward, is Flash ACTIONSCRIPT3.0,AS3 before is AS2. AS3 uses the ES4 syntax, now JavaScript, with the ES6 syntax. If remembered correctly, AS2 's grammar belongs to the JavaScript1.5 magic change.

ACTIONSCRIPT3 is Adobe's implementation of the JVM for performance reasons. In contrast, the current node. JS uses Chrome's V8.

The difference between 1.2 haxe and typescript

In fact, do not want to write this paragraph, but still for the sake of convenience, handy science. TS is a superset of JS, HX is a variant of AS3, has a kinship relationship. It is important to note that the HAXE3 syntax, compared to ES6 is actually lagging behind, but this does not affect the daily use, because Haxe's ecology is still enough stable.

1.1.1 Who's still using haxe?

These are the main categories: Flash Yilao, part of the older game developers, and part of the game developers who need to cross-platform. Refer to the official website for details.

1.0 RELATED LINKS

All of the main reference links covered in this article I first put in front, because it is convenient for me to retrieve.

Official website: https://haxe.org chat Room: https://gitter.im/HaxeFoundation/haxe

Online Test HX Code: https://try.haxe.org API Reference: https://api.haxe.org

Manual: Https://haxe.org/manual/introduction.html Library Index: https://lib.haxe.org

Reference/1:https://jcward.com/a+beginners+guide+to+hacking+haxe+macros for macros

Lime Command line: http://lime.software/docs/command-line-tools/basic-commands/

OPENFL Document: http://www.openfl.org/learn/

Above, for the time being. In addition, there are many Hong Kong in the answer chat room, and know quite a lot.

2. Hello World

To be ashamed, writing a Hello world with haxe is actually quite complicated.

2.1 Preparatory work

First you need to install Haxetoolkit, then you need an editor, here I choose Vscode. After installing Haxe, install the Haxe plug-in inside the Vscode.

The ontology of 2.2 Hello World

1 class Main {2     Static function Main () {3         Trace ("Hello, world!" ); 4     }5 }

The standard notation, in fact, in front of the main function to add a public. Then write the code into MAIN.HX, yes, as in Java, Haxe will also be consistent with the name of the same name/and the class name must start with uppercase. Moreover, Haxe3.4 currently does not support identifiers [identifier] in Chinese, it is said that 4.0 will be achieved, but I think, is not very necessary, after all, can also use pinyin.

2.3 How to run

This is the key to the whole hello world. The official tutorial [X1] gives the use of the Haxe command line:

Haxe-main Main--INTERP

To add this, the-main parameter actually means specifying the class/file that provides the main function. --interp is explaining the meaning of execution, haxetoolkit inside there is a virtual machine called neko (about this detail, don't be too concerned about it at first, know that Neko is a lightweight VM labeled Lua good).

2.3.1 How to generate EXE/command line methods

Haxe-main Main-cpp Build

The first compilation takes some time. It should also be compiled by the CL or MinGW gcc that needs to be MSBuild, but because it already has the vs2017 build tool, there is no separate test of the dependency problem of the compilation environment. When the compilation is complete, the command line runs Build\main.exe to see the results of the debug output.

Https://haxe.org/manual/target-cpp.html

2.3.2 standard policy for generating EXE

The standard strategy is: use Hxml to write out the compilation parameters separately, and then use haxe build.hxml to specify the corresponding hxml.

Https://haxe.org/manual/compiler-usage-hxml.html

2.3.3 besides turning into CPP and generating EXE, what are the output modes?

Personally, in addition to CPP, others are commonly used also have two kinds: output js, output into neko/and then use NEKOVM execution. Python, instead of generating a py code, prefers to call Python in Haxe. PHP and Java are rarely used by individuals, but they are a common scenario.

3. What should I do after Hello world?

At this point you have a lot of choices, first of all depending on what you want to do.

Is the schema above the haxe. Generally speaking, HAXE+OPENFL's relationship is very close to C # + WPF. Personally think Haxe is still suitable to do a bit more than JS, of course, this aspect typescript obviously more advantages, just haxe more than TS use a little more.

3.1 What am I going to do with haxe?

The first is to meet everyday needs. For example, used Python to write reptiles, the future will be replaced by Haxe to do. Similarly, partial typescript can also be substituted. Of course, in the end, I would like to call Erlang/elixir directly in Haxe, and kdb+/q. The second is used to pits, of course, small procedures with haxe to do, nor can not, but the preparation work seems a bit more, than directly with the Typescript+taro. Other pits, Erlang side can try to use Haxe to invoke, in one is to use haxe to implement a suitable programming input method.

3.2 What I can do to make sense of haxe

The first is the Haxe culture. The Chinese culture here does not mean to haxe the code/grammar/Documents of the Chinese language and so on, but to supplement the relevant reference for use. For example, the variable name, I want to haxe in the code to form a self-wording specification, convenient for later use as input method test.

4. Which pits did you step into when writing this article?

The first is the pit of the document. Haxe's API documentation can only be searched for class, and cannot be searched for function under class. It's best to use third-party search tools. For example, the trace () function in the first Hello World is actually in haxe. It's under log.

The second is the use of macros. I write less Java, so when I see the public static function, I would like to make a simplification, see if it can be written as PSF. After a round of tossing and finding, unless you change the AST, there is no other way. So finally gave up, in order to steal a little lazy to change ast, no need.

Again, because I want to redefine the existing library function name, so we need to do some preprocessing work with macro, so write a helper:

Import haxe.macro.Expr; class util{  publicstaticfunction Yin (e:array<expr>) {      Return  macro trace ($a {e});}   }

Here the import Expr, not strict requirements can not write. Also by the way try to write import in class, no, can only write class outside. For the use of macro, the main reference: Https://haxe.org/manual/macro-reification-expression.html. Here the $a{} of a, is the meaning of the array, Yin is printed pinyin.

Finally, because macro is the static function by default, it is found in inheritance that the static functions of class are not inherited. I went to Gitter and said it was designed so far (presumably considering the need to optimize the inline to other target). Then a German big guy said, "You can use macro to achieve this feature[x2]." I think about it, or use the previous, Python pyramid ZCA architecture traversal routines are relatively simple. Then wrote a backtracking/class function of the helper-> trystatic:

1  Public Static functiontrystatic (C,field) {2     varGsc=Type.getsuperclass;3     vargcf=Type.getclassfields;4     varRF =Reflect.field;5     varL=[C];varLflag=-1;6     7      while(lflag!=1){8       varD = l[l.length-1];9       if(d==NULL)returnRF (l[l.length-2],field);Ten       ElseL.push (GSC (d)); One       varD2 =GCF (d); A       varLflag =d2.indexof (field); -     } -     returnRF (l[l.length-1],field); the}

When this code was sent to Gitter, a Hong Kong Dalao said that the VAR definition of your 2/3/4 line was a bit superfluous, affecting the inline, which would affect performance. This reminds me, because this play although in the Py is OK, but there is no effect in CPP, I really say not good. But the test of two different strategies, the generated JS code is actually similar (feeling does not affect the JS performance). In one is he has mentioned the question of readability, saying that my RF () readability is not as good as the original. I say this is used with APL, the shorter the code/variable name the better, English readability is secondary (logical readability on the line).

1.3 Why did you choose Haxe?

First of all, typescript can not use a very concise way to generate stand alone EXE file, which is not suitable for me to teach. Second, Haxe's grammar is a bit outdated, but basically enough, some even better than TS. For example pattern matching,ts in order to be compatible with ES Standard, did not do the standard pattern match syntax, only did the destructure[1][2].

Secondly, Haxe's historical burden is small, less dependent, very light, and widely used. It can be said that he has been planning to use haxe instead of Python.

5. What do you write next?

The main writing haxe in the basic ideas of culture, other as the case to complement.

Tail Note:

[2] pattern matching under typescript: https://pattern-matching-with-typescript.alabor.me

[X2] https://github.com/maitag/peote-view-remaster/blob/master/src/peote/view/Element.hx#L215

Haxe: East Travels (Upper) Part1:intro

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.