What is Facebook's new Hack language?

Source: Internet
Author: User
Tags hhvm ocaml
Hack static type, with some advanced features (in comparison) such as anonymous functions. There are also asynchronous and so on. What we cannot bear is whether the variable name must be added with $. & Gt; Facebook is a family that is keen on using PHP, but BryanO Sullivan, head of the Hack team, also pointed out that since many developers are developing many different projects, it makes sense to be able to capture errors before running the program. This can make the developer's programming process more efficient, and make the program run more efficiently.

Static type, with some advanced features (in comparison) such as anonymous functions. There are also asynchronous and so on. What we cannot bear is whether the variable name must be added with $.

> Facebook is a family of PHP users, but Bryan O 'Sullivan, head of the Hack team, also pointed out that many developers are developing many different projects, it makes sense to be able to capture errors before running the program. This makes the developer's programming process more efficient, which is of the same significance as making the program run more efficiently. As a result, some senior Facebook engineers came together to develop the Hack language. Facebook has been using PHP and Hack internally for a year, and now has begun planning to port code to Hack as much as possible. Hack does not affect the running speed of the Code...

Via Hack: Facebook's new Programming Language Reply: The intern of another FB jumps out and says something. The following are my personal opinions.
I personally do not think that Hack is an independent language. The relationship between Hack and PHP is considered to be very similar to the relationship between ActionScript and ECMAScript (JavaScript. Despite the introduction of a large number of new elements (such as type checks), it still cannot change the fact that it retains the PHP syntax, habits, and standard library. Hack is more like a dialect (dialect)
Why does Facebook seem to be an independent language in publicity? I think the main consideration here is the respect for the PHP community and the avoidance of many PR issues. The compatibility issues left by the first browser war have scars the Internet world and left a lesson: no commercial company is allowed to hold a standard. Therefore, if Facebook uses Hack as a modified version of PHP, it may be a bit of a worry, fearing that PHP will become the world of Facebook.
It is difficult for the PHP community to satisfy Facebook's needs. I will not mention the importance of Type Safety. A very important requirement is coroutine. Facebook is an extremely IO heavy Application (imagine that every time you open the homepage, You need to arrange a feed, get the content of each story, who liked it, and the latest comment ...), therefore, Asynchronization is required. Previously Facebook's solution was this: Justin Mitchell's answer to Facebook Infrastructure: What are preparables and how are they implemented? The code is rather ugly. What is the best way to solve the asynchronous code structure? Coroutine! Refer to A Study on Solving Callbacks with JavaScript Generators . PHP 5.5 Finally introduced generators, But Facebook could not wait that long.
But obviously Facebook cannot be migrated to another language. Because the current Code "too old" is too ugly ", it is very naive to give up the existing code and try again. Refer to Things You shoshould Never Do, Part I . The central idea is simple: programmers who write their current Code are not stupid. In addition, it is likely that you will be smarter to add one.
I think Zuckerberg was wise to use PHP + MySQL. Facebook has a history of ten years. Recall what the status of July 2004 is? Django just came out, and Ruby on Rails will be around for nearly two years. Perl never got angry from beginning to end. ASP is dead, although still popular (at least in China ). Besides, although PHP is not a good language, it is easy to use on websites.
Therefore, the purpose of Hack is not to become a common language. If you want to create a new project, consider Python, RoR, or even NodeJS. It exists to solve a very special situation: You don't like PHP, but you still need to use it. The only thing I can think of is to maintain an existing large website. I don't know if Wikipedia is interested.
Finally, I don't think any language is actually bad. When too many people scold the language, they forget that the code is written by people. No good language can be written efficiently, easily read, and easily maintained by third-stream engineers. However, some languages present the illusion that sanstream engineers are Daniel (mom, I can write dynamic web pages !), This is also a major source of PHP's bad name. Why does Haskell sound tall? Because the third-stream engineer has never heard of it. It cannot be said that it is good or bad. What is the future? At least it is an optimization and experience for PHP. But at present, it is a little difficult to promote HHVM, and it is not generally complicated to install it. In addition, there is still a lot of cost to convert PHP to HH. It can only be said that it is okay to use it for the time being. Don't expect a large wave of botnets to attack soon.

Read the notes written after reading the hacklang document and read them for reference only:

  • Enhanced input and output types for PHP
    • More rigorous and secure writing
    • The nullable type (? Can be null)
    • Non-transparent type alias (you can assign a type value to a new name)
    • Class this pointer type (Child class can be inherited)
    • Array type enumeration (specifying the type order in the array)
    • Anonymous function type (this is useful for anonymous function parameter conventions)
  • Added the type (meaning it is literal if it is not explained)
    • Vector
    • Map
    • Set
    • Pair
    • Shape
    • Tuple
  • Class
    • Upgrade the input parameter to an internal variable.
    • Method distribution: static dynamic separation (this is so nice, native PHP, you can only write some strange names and use Magic for distribution)
    • Strict Class Inheritance Method override (to overwrite the parent class method, a declaration must be added)
  • Class Alias
    • External extension (=)
    • As syntax (Pass in the specified x as XXXXXXX)
    • <> Syntax
  • Simplified Lambda (for example, $ x ==>$ x * 2)
  • XHP syntax, used to output HTML and XML and automatically filter XSS (security is a very important thing for hacklang)
  • Trait (enhanced trait)
    • Require inheritance
    • Require implementation
  • Optimized generator and Async (I am looking forward to it, but I actually prefer the coroutine and Channel of Go)
    • Continuas (encapsulate the iterator and you can specify the iteration type)
    • Async (declared asynchronous functions can be executed asynchronously)
    • Awitable (the function that returns the Declaration can be executed using awit)
  • Discard PHP features
    • Global Code (Code cannot be written outside a function or class)
    • Function construction method with the same name as the class name (the class function name cannot be the same as the class name, otherwise an error will be thrown)
    • You are not allowed to call the parent static method (parent: xxxx, you are finished)
    • Static object-level calls (objects can only be called by objects->)
# Summary

Strictly defines input and output types for PHP, and adds several types and more secure HTML syntax. In short, most PHP supplements are not rigorous, and there are Async asynchronous syntaxes, but they are limited. Correct this. The anonymous function is introduced in PHP 5.4 (if you remember correctly ).
UPDATE: Check that PHP5.3 supports the anonymous function and then uses the $ this keyword in PHP5.4 to support closure. I always thought PHP5.4 was supported.

When I was promoting Hack internally in FB, I had an internship there. I hope the following things will not affect NDA.

Let's take a look at what Facebook has done on PHP:

According to someone from HN, Facebook has written: a compiler from PHP to C ++; a compiler from PHP to VM byte code; a corresponding runtime for each; extensions to PHP; a type checker, and an inference engine. now they have to created a new programming language, which is already red by PHP.

Lesson learned: start with a decent programming language.


Why does Facebook charge weekly stamps for an Hack? Isn't it because the historical code library is too large to be improved based on PHP itself? It is ridiculous that many people use "Facebook is using PHP" as a good language argument...

Looking back, Hack mainly wants to solve these problems:
  1. Efficiency
  2. Provides code check in the compilation age for large projects
  3. Provides a solution for gradual transition to new languages in the face of huge existing code Libraries

To solve these problems, the corresponding policy is:

  1. Hack is a typed language that increases the JIT optimization space of HHVM.
  2. Same as above, Hack is a typed language and comes with type inference to provide code check in the compilation age

Because it is necessary to migrate a large amount of code inside the FB to the Hack, many basic things cannot be changed. If you add $ to remove the variable name... It is better to rewrite the code in other languages. Why should we continue to use Hack?

HHVM is the future of PHP...


Gossip


  1. The Hack language is discussed outside of FB on a con of OCaml last year (Sorry, the original link cannot be found ). Yes, Hack is written in OCaml.
  2. To avoid NDA violation, I will post a link http://spyder.wordpress.com/2014/03/16/why-ocaml-why-now/Keyword: "web IDE 』. It seems that this item is not open source yet, but it should be faster.
Php is a good news. Although there are ruby/py and so on, php has a long history in website development and many projects are still in the mainstream. Scala is an upgrade to java, java is also being upgraded, and hack is also an upgrade to php.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.