Hack,一种开源脚本语言,运行在HHVM虚拟机上,主要开发者为Facebook。在2014年3月20日正式发布。在发布前,Facebook已经在它的网站上广泛使用及测试。Hack语言官方网址是http://hacklang.org/ (chinafix.net注:英文版)
Hack is a programming language 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.
Hack provides instantaneous type checking via a local server that watches the filesystem. It typically runs in less than 200 milliseconds, making it easy to integrate into your development workflow without introducing a noticeable delay.
下面是几段有趣的Hack代码:(chinafix.net 附注)
<?hh
class MyClass {
public function alpha(): int {
return 1;
}
public function beta(): string {
return 'hi test';
}
}
function f(MyClass $my_inst): string {
// Fix me!
return $my_inst->alpha();
}
...
<?hh
class MyClass {
const int MyConst = 0;
private string $x = '';
public function increment(int $x): int {
$y = $x + 1;
return $y;
}
}
...
<?hh
class Box<T> {
protected T $data;
public function __construct(T $data) {
$this->data = $data;
}
public function getData(): T {
return $this->data;
}
}
...
<?hh
function foo(): (function(string): string) {
$x = 'bar';
return $y ==> $x . $y;
}
function test(): void {
$fn = foo();
echo $fn('baz'); // barbaz
}
为了让程序员高效率地写代码和测试,Facebook的三位工程师Bryan O’Sullivan、Julien Verlaguet和Alok Menghrajani发明了一种新的编程语言——Hack。该语言在公司内部使用了一年时间,今天,Facebook正式将它开源与大家分享,鼓励大家使用希望一起来改进它。
软件的世界里到处都是编程语言,也不断会有新的编程语言出现。但是Hack的出现,立即让其他语言显得黯然失色。Hack加入了现代编程语言的特性,既可以实现PHP的快速开发,又通过结合一些静态语言元素的方式让程序员们在程序运行之前就可以发现错误。
大约在2003年末,Facebook创始人Mark Zuckerberg选择使用在当时很流行的PHP编程语言来建立网站,这种语言不需要程序员花时间定义程序中每个变量所需要的特定参数。接下来的十年时间里,Mark Zuckerberg用自己“The Hacker Way”的编码理念不断鼓励工程师们寻找改善技术的途径,随着Facebook发展壮大,PHP语言开始显示出其局限性,需要更多的服务器来运行网站。
当规模大了以后,最好使用像Java类的静态语言,这样更容易管理。去年,Facebook的三名工程师为了解决服务器问题,创建运行所有PHP代码的虚拟机HHVM。为了进一步提高开发人员的效率,他们创造了一门新的编程语言,Hack就此诞生。Hack基于PHP,支持静态和动态语言,相比动态语言编程更安全,也更易于检查和管理。
你可以认为Hack是PHP的新版本,它结合了动态和静态编程语言各自的优点,这种语言大大提高了程序员的编程效率,同时缓解了公司服务器的压力。目前,Facebook系统的PHP代码已全部用Hack重构完成。
有兴趣?联系admin@chinafix.net,一起学习!