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 FaceBook
team which is based on 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…
Basically, Heck
is an improvement of PHP
while 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
or C++
with dynamic-type languages like PHP
.
Let’s see an example of PHP
function:
Now let's see another example of same function in
Heck
The difference is pretty straightforward,
Heck
provides static type casting facility in function parameters and return type but inPHP
we can't do it but yes,PHP - 5
introduces 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 inPHP
, anyways.So, this is better to be able to use static type casting and this is the principal addition in
Heck
along 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),Hack
is 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
Hack
will still look and feel like the dynamic language to whichPHP
programmers 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:
Hack
is based onHHVM
dynamic compilation or just-in-time compilation(JIT)
engine. AJIT
compiler 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
JIT
compilation 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
PHP
is 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 likePHP
, 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 currentPHP
codebases. Read more aboutHeck
, it's features with examples, tutorial and documentation on hacklang.org.Is this a replacement for
PHP
:Well, honestly I think, it's been built to replace the
PHP
, it's primary goal was to use the dynamic nature ofPHP
but with some improvements such as early error detection is one of those improvements and hence,Heck
has just combined the static language feature like type casting with PHP's existing features, it just reconciles the fast development cycle ofPHP
with the discipline provided by static typing, while adding many features commonly found in other modern programming languages. In fact, mostPHP
files are already validHack
files. Hence,Hack
is a programming language forHHVM
that interoperates seamlessly withPHP
butHeck
is still notPHP
.Whether it'll replace the
PHP
or not, it depends on developers. If developers adoptHeck
overPHP
then it may replace it but there are so many reasons that developers may not adopt it despite of being an improved language similar toPHP
. It only runs onHHVM
whilePHP
runs on many platforms.HHVM
still doesn't run onWindows
but they are working on it, maybe soon it'll be available forWindows
as well. Since most developers use third party libraries and frameworks to build applications/websites usingPHP
, in this caseHeck
is a new language and there are no such resources (third party libraries and frameworks) available to work withHeck
. Also, hosting providers (in case of shared hosting) have to provide such an environment forHeck
to run because withoutHHVM
it won't work butPHP
is everywhere. Finally, In my personal opinion,It's true that,
Heck
has some better features thatPHP
doesn't have butPHP
is also getting better day by day and I believe thePHP
developer community is also working hard to improve the language and this is a continuous process so what ifPHP
adds these features in future versions of it. SincePHP - 6
is released and have some very cool and interesting improvements but is that allPHP
got? Definitely not, by the timePHP
will improve as well and maybe it'll remain as a superior one, who knows!As a developer, I definitely like
Heck
because of it's similarity withPHP
while 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 ofHeck
to 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 thatGithub
page, it's a Heroku Buildpack for HHVM. Just read the instructions and you'll be able to test it, good forWindows
users.If you are not familiar with
Heroku
then 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 someHeck
code. You may also check the tutorial onHeck
website and you may also read Bootstrapping a Project. Happy Hacking...N/B: Article based on Heck website.