# Hack（编程语言）

**Hack** 是 Facebook（现为 Meta）为 HipHop 虚拟机（HHVM）开发的一种渐进类型编程语言，于 2014 年 3 月 20 日发布并开源。它脱胎于 PHP：发布时大多数 PHP 文件已经是合法的 Hack 文件，该语言的设计目标是让庞大的 PHP 代码库能够逐个文件地采用静态类型，而无需重写。<sup>[1](https://engineering.fb.com/2014/03/20/developer-tools/hack-a-new-programming-language-for-hhvm/)</sup>

| 关键事实 | 详情 |
|---|---|
| 首次发布 | 2014 年 3 月 20 日，由 Facebook 为 HHVM 宣布并开源<sup>[1](https://engineering.fb.com/2014/03/20/developer-tools/hack-a-new-programming-language-for-hhvm/)</sup> |
| 类型系统 | 渐进类型：动态类型代码与静态类型代码可无缝互操作，即使在单个文件内类型检查也是增量的<sup>[1](https://engineering.fb.com/2014/03/20/developer-tools/hack-a-new-programming-language-for-hhvm/)</sup> |
| 类型系统特性 | 泛型、可空类型、类型别名，以及对类型参数的约束<sup>[1](https://engineering.fb.com/2014/03/20/developer-tools/hack-a-new-programming-language-for-hhvm/)</sup> |
| 运行时 | HHVM，一个运行 Hack 程序的开源虚拟机<sup>[2](https://github.com/facebook/hhvm)</sup> |
| 类型检查器 | 一个“全程序”检查器，具备增量性、并行性，以 OCaml 实现<sup>[3](https://wg28-2018.mpi-sws.org/technical-presentations/files/cfdb7_uploads/1544776261-HackFlowAsilomar.pdf)</sup> |
| PHP 兼容性 | 在 HHVM 4.0（2019 年 2 月 11 日）中放弃，该版本增加了对 `.hack` 文件的支持并移除了若干 PHP 行为<sup>[4](https://hhvm.com/blog/2019/02/11/hhvm-4.0.0.html)</sup> |
| 知名用户 | Facebook——2014 年 Hack 已驱动该公司网站的几乎全部内容；Slack——其后端以 Hack 编写并运行于 HHVM<sup>[5](https://www.wired.com/2014/03/facebook-hack/)</sup><sup> • </sup><sup>[6](https://slack.engineering/remote-development-at-slack/)</sup> |

## 起源与目的

Hack 的诞生是为了调和其设计者所说的 PHP 快速开发周期与静态类型所提供的纪律性，同时添加其他现代编程语言中常见的特性。<sup>[1](https://engineering.fb.com/2014/03/20/developer-tools/hack-a-new-programming-language-for-hhvm/)</sup> Facebook 此前已将 PHP 工作负载迁移到 HHVM（HipHop 虚拟机）以提升性能；Hack 构建于该运行时之上。<sup>[5](https://www.wired.com/2014/03/facebook-hack/)</sup>

迁移路径是经过深思熟虑的设计选择。团队为函数签名和类成员标注类型信息，让类型检查器推断其余部分，从而在单个文件内，部分代码可以转换为 Hack，其余部分仍保持动态类型。从技术上讲，Hack 是一种“渐进类型”语言，动态类型代码与静态类型代码可无缝互操作。<sup>[1](https://engineering.fb.com/2014/03/20/developer-tools/hack-a-new-programming-language-for-hhvm/)</sup> 少量已弃用的 PHP 函数和与静态类型不兼容的特性（如“可变变量”和 `extract()` 函数）不受支持。<sup>[1](https://engineering.fb.com/2014/03/20/developer-tools/hack-a-new-programming-language-for-hhvm/)</sup>

## 类型系统

Hack 包含一个强静态类型系统，其文档除类型检查器的基础知识外，还涵盖子类型等特性。<sup>[7](https://docs.hhvm.com/hack/types/introduction/)</sup> 除发布时宣布的特性外，后续添加的特性专门服务于迁移：`dynamic` 类型旨在帮助代码从无类型模式过渡到严格模式，让检查器从可用信息中推断出尽可能最佳的类型，并允许 `dynamic` 值在强制转换点被强制转换为可在运行时检查的类型。与子类型不同，这种强制转换不具有传递性，因此 `int ~> dynamic` 和 `dynamic ~> string` 并不意味着 `int ~> string`。<sup>[8](https://docs.hhvm.com/hack/built-in-types/dynamic/)</sup>

检查器还能理解 `is` 和 `as` 运算符，因此能从中推断出精确的类型，Hack 还提供类型谓词函数，如 `is_int` 和 `is_bool`。<sup>[9](https://docs.hhvm.com/hack/expressions-and-operators/type-assertions/)</sup>

## 执行与工具

Hack 程序运行在 HHVM 上——一个基于字节码、JIT 编译的运行时——并由 Hack 的全程序类型检查器进行检查，该检查器具备增量性、并行性，以 OCaml 实现。<sup>[3](https://wg28-2018.mpi-sws.org/technical-presentations/files/cfdb7_uploads/1544776261-HackFlowAsilomar.pdf)</sup> HHVM 软件包包含了使用 Hack 所需的一切，包括运行时和类型检查器。<sup>[10](https://docs.hhvm.com/hack/getting-started/quick-start/)</sup> 2014 年发布了 vim、emacs 和 [Sublime Text](https://www.edgepedia.org/zh/sublime-text) 的编辑器插件。<sup>[11](https://hhvm.com/blog/6443/hack-recent-updates)</sup>

源文件历史上使用 `.php` 扩展名，这在较旧的 Hack 代码中仍然存在，但不推荐用于新代码；`.hack` 扩展名随 HHVM 4.0 一同到来。<sup>[12](https://docs.hhvm.com/hack/source-code-fundamentals/program-structure/)</sup><sup> • </sup><sup>[4](https://hhvm.com/blog/2019/02/11/hhvm-4.0.0.html)</sup>

## 与 PHP 的关系

发布时，Hack 与运行在 HHVM 上的 PHP 向后兼容，Hack 代码可与之无缝集成。<sup>[1](https://engineering.fb.com/2014/03/20/developer-tools/hack-a-new-programming-language-for-hhvm/)</sup> 2014 年 11 月，Facebook 宣布了一个实验性转译器 `h2tp`（“HH（Hack）转译器”），它允许已从 PHP 转换为 Hack 的项目仍然发布面向 PHP 语言的版本。<sup>[13](https://engineering.fb.com/2014/11/11/developer-tools/announcing-the-hack-transpiler/)</sup>

此后两种语言分道扬镳。2018 年 9 月，HHVM 团队宣布终止对 PHP 的支持，2019 年 2 月 11 日发布的 HHVM 4.0 不再以兼容 PHP 为目标。其与 PHP 不兼容的变更移除了 PHP 数组中有而 Hack 数组或 Hack 集合中没有的行为，弃用了引用，并移除了检查或修改调用者栈帧的函数，如 `compact()`、`extract()`、`func_get_args()` 以及单参数的 `parse_str()`。<sup>[4](https://hhvm.com/blog/2019/02/11/hhvm-4.0.0.html)</sup> 到 2020 年的 HHVM 4.71，PHP 文件的行为被统一，因为 HHVM 自 4.0 起已不再支持 PHP。<sup>[14](https://hhvm.com/blog/2020/08/18/hhvm-4.71.html)</sup>

## 采用情况

在 Facebook 内部，发布时 Hack 已驱动该公司网站的几乎全部内容，该网站服务超过 12 亿人。<sup>[5](https://www.wired.com/2014/03/facebook-hack/)</sup> 2014 年 4 月，在门洛帕克 Facebook 园区举办的 Hack 开发者日上，该语言被介绍给更广泛的社区，活动吸引了 150 多名 PHP 和开发者社区成员亲临现场，以及 2000 多人在线参与。<sup>[15](https://hhvm.com/blog/4685/hack-developer-day-2014-keep-hacking)</sup><sup> • </sup><sup>[16](https://engineering.fb.com/2014/04/10/developer-tools/hack-developer-day-recap/)</sup>

在 Facebook 之外，Slack 是有记录的主要用户：其后端以 Hack 编写并运行于 HHVM，Slack 的工程师描述了在 API 调用中使用 Hack 的强类型来处理可变载荷结构，以及在各种用例中使用形状类型。<sup>[6](https://slack.engineering/remote-development-at-slack/)</sup><sup> • </sup><sup>[17](https://slack.engineering/streamlining-your-workflow-debugging-for-hhvm/)</sup><sup> • </sup><sup>[18](https://slack.engineering/hacklang-at-slack-a-better-php/)</sup>

Hack 也作为渐进类型的突出范例被编程语言研究领域所研究，与 [TypeScript](https://www.edgepedia.org/zh/typescript) 和 Racket 的 Typed Racket 等语言并列。<sup>[19](https://www.cl.cam.ac.uk/teaching/2021/ConceptsPL/lectures.pdf)</sup> Facebook 还另外创建了 Flow——一种用于 [JavaScript](https://www.edgepedia.org/zh/javascript) 的可选类型语言，并继续开发两者。<sup>[20](https://users.cs.utah.edu/~blg/cv/failures/facebook-2015-reject.pdf)</sup>

## References

1. [Hack: a new programming language for HHVM, Engineering at Meta (March 20, 2014)](https://engineering.fb.com/2014/03/20/developer-tools/hack-a-new-programming-language-for-hhvm/)
2. [facebook/hhvm on GitHub](https://github.com/facebook/hhvm)
3. [HackFlow presentation slides, MPI-SWS](https://wg28-2018.mpi-sws.org/technical-presentations/files/cfdb7_uploads/1544776261-HackFlowAsilomar.pdf)
4. [HHVM 4.0.0, HHVM blog (February 11, 2019)](https://hhvm.com/blog/2019/02/11/hhvm-4.0.0.html)
5. [Facebook Introduces 'Hack,' the Programming Language of the Future, WIRED (March 20, 2014)](https://www.wired.com/2014/03/facebook-hack/)
6. [Remote Development at Slack, Slack Engineering](https://slack.engineering/remote-development-at-slack/)
7. [Introduction to Hack types, Hack & HHVM Documentation](https://docs.hhvm.com/hack/types/introduction/)
8. [Dynamic type, Hack & HHVM Documentation](https://docs.hhvm.com/hack/built-in-types/dynamic/)
9. [Type Assertions, Hack & HHVM Documentation](https://docs.hhvm.com/hack/expressions-and-operators/type-assertions/)
10. [Quick Start, Hack & HHVM Documentation](https://docs.hhvm.com/hack/getting-started/quick-start/)
11. [Hack: Recent Updates, HHVM blog (October 21, 2014)](https://hhvm.com/blog/6443/hack-recent-updates)
12. [Program Structure, Hack & HHVM Documentation](https://docs.hhvm.com/hack/source-code-fundamentals/program-structure/)
13. [Announcing the Hack Transpiler, Engineering at Meta (November 11, 2014)](https://engineering.fb.com/2014/11/11/developer-tools/announcing-the-hack-transpiler/)
14. [HHVM 4.71, HHVM blog (August 18, 2020)](https://hhvm.com/blog/2020/08/18/hhvm-4.71.html)
15. [Hack Developer Day 2014: Keep Hacking, HHVM blog](https://hhvm.com/blog/4685/hack-developer-day-2014-keep-hacking)
16. [Hack Developer Day Recap, Engineering at Meta (April 10, 2014)](https://engineering.fb.com/2014/04/10/developer-tools/hack-developer-day-recap/)
17. [Streamlining Your Workflow: Debugging for HHVM, Slack Engineering](https://slack.engineering/streamlining-your-workflow-debugging-for-hhvm/)
18. [Hacklang at Slack: A Better PHP, Slack Engineering](https://slack.engineering/hacklang-at-slack-a-better-php/)
19. [Concepts in Programming Languages lecture notes, University of Cambridge](https://www.cl.cam.ac.uk/teaching/2021/ConceptsPL/lectures.pdf)
20. [Ben Greenman, Sound & Performant Gradual Typing (Facebook 2015 proposal document)](https://users.cs.utah.edu/~blg/cv/failures/facebook-2015-reject.pdf)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Programming languages › Individual programming languages › C-family and imperative languages

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/hack-programming-language

English original: https://www.edgepedia.org/hack-programming-language
