Tuesday, 9 June 2015

HB Blog 77: Hack - A Programming Language Created By Facebook.

Hack is a programming language for the HipHop Virtual Machine (HHVM), created by Facebook as a dialect of PHP. Hack allows programmers to use both dynamic typing and static typing. This kind of a type system is called gradual typing.
HHVM is an open-source virtual machine designed for executing programs written in Hack and PHP. HHVM uses a just-in-time (JIT) compilation approach to achieve superior performance while maintaining the development flexibility that PHP provides.
Starting with Hack language setup as follows:-
1)Install HHVM
Executing the following 4 commands from the command line will have HHVM installed and ready:
  1. wget -O - http://dl.hhvm.com/conf/hhvm.gpg.key | apt-key add -
  2. echo deb http://dl.hhvm.com/ubuntu saucy main | tee /etc/apt/sources.list.d/hhvm.list
  3. apt-get update
  4. apt-get install hhvm
To confirm that HHVM has been installed, type the following command:
hhvm --help
You should have the latest version of HHVM installed, have a php.ini or config.hdf set up if necessary, and HHVM running as a webserver serving your project.

2)Creating the project structure for app and getting the type checker running.
In the root of your project directory, create an empty file named .hhconfig. The type checker uses this file to know what directory tree to type check without needing to specify paths to every command.
Now, run hh_client. This will start up the type checking daemon, wait for it to finish checking the current directory tree, and report any errors that it found.
projects/
├── project-1/
│   └── app/
│       └── .hhconfig
├── project-2/
│   └── app/
└── .hhconfig
 3)Start Hack language and run hello world program as follows.
Hack language is preety much similar to PHP, for example use <?hh at the top of your file similar to php code <?php and save file with extension .hh. You can also save the file with ,php but it is better for distinguish hack files with php files.

1
2
3
4
5
<?hh
// void is used to indicate that a function does not return anything.
function say_hello(): void {
  echo "hello world";
} 

No comments:

Post a Comment