What The Heck Is Hack
Well, first of all it has nothing to do with hacking but it’s a new open-source programming language developed by
PHP and runs on
HHVM (Hip Hop Virtual Machine), the FaceBook’s virtual machine which also supports
PHP as well.
Hack: a new programming language for HHVM announced by FaceBook
Today we’re releasing Hack, a programming language we developed for HHVM that interoperates seamlessly with PHP. Hack reconciles the fast development cycle of PHP with the discipline provided by static typing, while adding many features commonly found in other modern programming languages. Read More…
Heck is an improvement of
PHP is a dynamic-type language and
Hack is a combination of both dynamic and static type language because
Heck combined the elements of static-type programming languages such as
C++ with dynamic-type languages like
Let’s see an example of
Now let's see another example of same function in
The difference is pretty straightforward,
Heckprovides static type casting facility in function parameters and return type but in
PHPwe can't do it but yes,
PHP - 5introduces type hinting but only using a class/object but we can't type cast any scalar values (int, string etc) and there is no way to type cast the return value of a function or class method in
So, this is better to be able to use static type casting and this is the principal addition in
Heckalong with other features. Facebook team has developed a system to annotate function signatures and class members with type information; the type checking algorithm (the “type checker”) infers the rest. Type checking is incremental, such that even within a single file some code can be converted to Hack while the rest remains dynamically typed. So technically (according to Facebook),
Hackis a “gradually typed language: dynamically typed code interoperates seamlessly with statically typed code.
Within Hack's type system, several features has been introduced such as generics, nullable types, type aliasing, and constraints on type parameters. These new language features are unobtrusive, so the code you write with
Hackwill still look and feel like the dynamic language to which
PHPprogrammers are accustomed.
Why statically typed language is better than dynamically typed language:
Basically dynamically typed languages allows for rapid prototyping, more concise code, and a lot of flexibility. Dynamic typing also comes with a cost. Errors are only caught at run time. There is no compile-time analysis for code optimization. On the other hand, with statically typed languages, errors can be caught before run time. Code becomes more readable and self-explaining and bug free before before it goes to live.
How it works:
Hackis based on
HHVMdynamic compilation or just-in-time compilation
JITcompiler runs after the program has started and it compiles the bytecode (already compiled to a form of nearly machine code, before runtime) on the fly into machine code.
A common implementation of JIT compilation is to first have AOT (Ahead of Time) compilation to bytecode (virtual machine code), known as bytecode compilation, and then have
JITcompilation to machine code (dynamic compilation), rather than interpretation of the bytecode. This improves the runtime performance compared to interpretation, at the cost of lag due to compilation. Read more...
Since one of the main issues with
PHPis that, coding errors are not detected until the script gone live, which is too late but, by combining the features of both static and dynamic languages; programmers can retain all the speed they'd have with a dynamic language like
PHP, while also it's possible to catch mistakes before runtime and it was done by the "type checker" which runs as a local server and watches the file system. The server keeps all information about the source code in memory and automatically updates itself when a file changes on disk. The type checker typically runs in less than 200 milliseconds and rarely takes more than a second, making it easy to integrate into the development workflow without introducing a noticeable delay.
Hack helps bridge the gap between dynamically and statically typed languages by providing features normally seen in statically typed languages to
PHP. The primary goal of Hack was to bring these features but remain as compatible as possible with current
PHPcodebases. Read more about
Heck, it's features with examples, tutorial and documentation on hacklang.org.
Is this a replacement for
Well, honestly I think, it's been built to replace the
PHP, it's primary goal was to use the dynamic nature of
PHPbut with some improvements such as early error detection is one of those improvements and hence,
Heckhas just combined the static language feature like type casting with PHP's existing features, it just reconciles the fast development cycle of
PHPwith the discipline provided by static typing, while adding many features commonly found in other modern programming languages. In fact, most
PHPfiles are already valid
Hackis a programming language for
HHVMthat interoperates seamlessly with
Heckis still not
Whether it'll replace the
PHPor not, it depends on developers. If developers adopt
PHPthen it may replace it but there are so many reasons that developers may not adopt it despite of being an improved language similar to
PHP. It only runs on
PHPruns on many platforms.
HHVMstill doesn't run on
Windowsbut they are working on it, maybe soon it'll be available for
Windowsas well. Since most developers use third party libraries and frameworks to build applications/websites using
PHP, in this case
Heckis a new language and there are no such resources (third party libraries and frameworks) available to work with
Heck. Also, hosting providers (in case of shared hosting) have to provide such an environment for
Heckto run because without
HHVMit won't work but
PHPis everywhere. Finally, In my personal opinion,
It's true that,
Heckhas some better features that
PHPdoesn't have but
PHPis also getting better day by day and I believe the
PHPdeveloper community is also working hard to improve the language and this is a continuous process so what if
PHPadds these features in future versions of it. Since
PHP - 6is released and have some very cool and interesting improvements but is that all
PHPgot? Definitely not, by the time
PHPwill improve as well and maybe it'll remain as a superior one, who knows!
As a developer, I definitely like
Heckbecause of it's similarity with
PHPwhile it added new features like other static typed language but only these are not enough to switch from one language to another, developers need more resources and support and a supportive community as well. So, maybe it's not the time to migrate but it's a good idea to give it a try, it doesn't require much effort. If anyone wants to give it a try then at first go to the the home page of
Heckto get an overview then navigate to Installation and Configuration from Docs and also check Installation on Cloud Computing platforms, there you can find HHVM installs on the » Heroku cloud platform, just visit that
Githubpage, it's a Heroku Buildpack for HHVM. Just read the instructions and you'll be able to test it, good for
If you are not familiar with
Herokuthen check the Heroku Dev Center, it's very easy to signup and get running. If you don't have an account on Heroku then create one first, it's simple and then follow the simple four steps on Getting Started with Heroku. Just create simple index page and run some
Heckcode. You may also check the tutorial on
Heckwebsite and you may also read Bootstrapping a Project. Happy Hacking...
N/B: Article based on Heck website.