CSC/ECE 517 Spring 2015/ch1a 20 HA: Difference between revisions
No edit summary |
|||
(54 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
<font size="6"><b> Hack </b></font> | |||
Hack is a language for [http://en.wikipedia.org/wiki/HipHop_Virtual_Machine HHVM] that interoperates seamlessly with [https://en.wikipedia.org/wiki/PHP PHP]. The | [[File:hack.jpg|frame|Source: https://rajesh38.wordpress.com/2014/03/23/facebook-unveils-new-programming-language-hack/|right]] | ||
[https://docs.google.com/document/d/1TgBtp7flIPKJwkkShgtcIkt--mtHuwVHsQX6Tpzj1rc/edit Topics for Writing Assignment 1a, Spring 2015] | |||
[http://en.wikipedia.org/wiki/Hack_%28programming_language%29 Hack (programming language)] | |||
Hack is a language for [http://en.wikipedia.org/wiki/HipHop_Virtual_Machine HHVM] that interoperates seamlessly with [https://en.wikipedia.org/wiki/PHP 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. | |||
The goal of Hack is to offer developers a way to write cleaner, safer and refactorable code while trying to maintain a level of compatibility with current PHP codebases. The primary way that this goal is achieved is to provide developers a way to annotate PHP functions and classes with type information, providing a type checking tool to validate those annotations. | |||
==History== | ==History== | ||
Line 47: | Line 32: | ||
O’Sullivan and company publicly revealed their new language this morning, and at the same time, they “open sourced” it, sharing the technology with the world at large and encouraging others not only to use it, but to help improve it. | O’Sullivan and company publicly revealed their new language this morning, and at the same time, they “open sourced” it, sharing the technology with the world at large and encouraging others not only to use it, but to help improve it. | ||
The software world is littered with programming languages, and new ones appear all the time. But according to some who have used it or who know the past work of those who built it, Hack has a design and a pedigree that immediately set it apart. “If Bryan O’Sullivan built it,” says programming guru David Pollak, who only yesterday heard about the new language, “I would walk across hot coals to use it.”<ref> CADE METZ, [http://www.wired.com/2014/03/facebook-hack/ Facebook Introduces ‘Hack,’ the Programming Language of the Future] 03.20.14</ref> | The software world is littered with programming languages, and new ones appear all the time. But according to some who have used it or who know the past work of those who built it, Hack has a design and a pedigree that immediately set it apart. “If Bryan O’Sullivan built it,” says programming guru David Pollak, who only yesterday heard about the new language, “I would walk across hot coals to use it.”<ref> CADE METZ, [http://www.wired.com/2014/03/facebook-hack/ "Facebook Introduces ‘Hack,’ the Programming Language of the Future"] 03.20.14</ref> | ||
==Features== | ==Features== | ||
Hack interoperates seamlessly with PHP, which is a widely used open source general-purpose scripting language that is especially suited for web development and can be embedded into [http://en.wikipedia.org/wiki/HTML HTML]. A majority of the valid PHP scripts is also valid in Hack; however, numerous less frequently used PHP features and language constructs are not supported in Hack.<ref> 1997-2015 the PHP Documentation Group, 2014-2015 Facebook[http://docs.hhvm.com/manual/en/hack.unsupported.php Hack Manual section- Hack and HHVM: Unsupported PHP Features in Hack] 2014-04-02</ref> | Hack interoperates seamlessly with PHP, which is a widely used open source general-purpose scripting language that is especially suited for web development and can be embedded into [http://en.wikipedia.org/wiki/HTML HTML]. A majority of the valid PHP scripts is also valid in Hack; however, numerous less frequently used PHP features and language constructs are not supported in Hack.<ref> 1997-2015 the PHP Documentation Group, 2014-2015 Facebook [http://docs.hhvm.com/manual/en/hack.unsupported.php "Hack Manual section- Hack and HHVM: Unsupported PHP Features in Hack"] 2014-04-02</ref> | ||
Hack extends the [http://en.wikipedia.org/w/index.php?title=Type_hinting&redirect=no type hinting] available in PHP 5 through the introduction of static typing, by adding new type hints (for example, for scalar types such as integer or string), as well as by extending the use of type hints (for example, for class properties or function return values). However, types of local variables cannot be specified. Since Hack uses a gradual typing system, in the default mode, type annotations are not mandatory even in places they cannot be inferred; the type system will assume the author is correct and admit the code.<ref> 1997-2015 the PHP Documentation Group, 2014-2015 Facebook [http://docs.hhvm.com/manual/en/hack.modes.partial.php "Hack Manual section-Partial Mode"] 2015-02-02</ref> However, a "strict" mode is available which requires such annotations, and thus enforces fully sound code.<ref> 1997-2015 the PHP Documentation Group, 2014-2015 Facebook [http://docs.hhvm.com/manual/en/hack.modes.strict.php "Hack Manual section-Strict Mode"] 2015-02-02</ref> | |||
In addition,Hack provides the following, non-exhaustive list of features: | In addition,Hack provides the following, non-exhaustive list of features: | ||
Line 61: | Line 49: | ||
* Type-safe collections (Vector, Map, Set). | * Type-safe collections (Vector, Map, Set). | ||
* A tool to check the types (the Hack type checker). | * A tool to check the types (the Hack type checker). | ||
Hack has been designed with PHP compatibility in mind. The type checker enforces checks on typed code and assumes calls to/from PHP and other untyped code are correct. This means the amount of type checking will gradually increase as more and more code is written in Hack. | Hack has been designed with PHP compatibility in mind. The type checker enforces checks on typed code and assumes calls to/from PHP and other untyped code are correct. This means the amount of type checking will gradually increase as more and more code is written in Hack. | ||
Hack is unlike many other statically typed languages: | Hack is unlike many other statically typed languages: | ||
Line 73: | Line 63: | ||
* Hack comes with the following benefits (and possible benefits): | * Hack comes with the following benefits (and possible benefits): | ||
* Return types makes the code more readable. | * Return types makes the code more readable. | ||
* Some typing mistakes will be caught by the type checker. | |||
* Better emacs/vim/etc. integration. | |||
* Future: Reliable refactoring tools. | |||
* Future: Most typing mistakes will be caught statically. | |||
===Comparison between Hack and PHP=== | |||
Hack is a language for HHVM. Introduction of HHVM makes PHP's fast performance many times faster. Moving your PHP code from PHP5 to HHVM is likely to result in a significant speedup.But the execution speed is the point of Hack -- the main point is about developer efficiency. Going from PHP on HHVM to Hack on HHVM, the gain is developer productivity<ref>Josh Watzman,[http://stackoverflow.com/questions/26097851/is-code-written-in-hack-faster-than-code-written-in-php-on-hhvm "Is code written in Hack faster than code written in PHP on HHVM?"] 2014-09</ref> | |||
One can also say that Hack is like a “PHP++”. It adds optional static typing and generics to PHP. But at the same time, it doesn't have ubiquity, maturity, stability like PHP.<ref>Marco Arment [http://www.marco.org/2014/03/21/hack "HACK isn't PHP] 2014-3</ref> | |||
== Syntax and semantics == | == Syntax and semantics == | ||
Line 136: | Line 130: | ||
===New Collection Types=== | ===New Collection Types=== | ||
Hack introduces new collection types: Vector, Set and Map. One is mentioned here. | |||
====Vector==== | ====Vector==== | ||
Vector is preferred over array. | Vector is preferred over array. | ||
Line 170: | Line 165: | ||
} | } | ||
</pre> | </pre> | ||
All the members of a class must be initialized. | All the members of a class must be initialized. There is also a shorter way to do the same: | ||
<pre> | |||
<?hh | |||
class Point { | |||
public function __construct( | |||
{ | private float $x, | ||
private float $y | |||
) {} | |||
} | |||
</pre> | |||
All the code snippets are referred from the source:<ref>Facebook [http://hacklang.org/tutorial "Hack tutorial"] 2005</ref> | |||
== References == | == References == | ||
Line 184: | Line 182: | ||
== External links == | == External links == | ||
* | * [http://en.wikipedia.org/wiki/Template:Official_website Template:Official_website] | ||
* [http://docs.hhvm.com/manual/en/hack.intro.php Hack language reference] | * [http://docs.hhvm.com/manual/en/hack.intro.php Hack language reference] | ||
* [http://www.infoworld.com/t/php-web/facebook-qa-hack-brings-static-typing-php-world-239112?page=0,1 Facebook Q&A: Hack brings static typing to PHP world] | * [http://www.infoworld.com/t/php-web/facebook-qa-hack-brings-static-typing-php-world-239112?page=0,1 Facebook Q&A: Hack brings static typing to PHP world] | ||
* [http://hacklang.org Full presentation on "Hack Dev Day"] | * [http://hacklang.org Full presentation on "Hack Dev Day"] | ||
[ | Category:[http://en.wikipedia.org/wiki/Category:Dynamically_typed_programming_languages Dynamically typed programming languages][http://en.wikipedia.org/wiki/Category:Facebook Facebook][http://en.wikipedia.org/wiki/Category:PHP_programming_language PHP_programming_language][http://en.wikipedia.org/wiki/Category:Scripting_languages Scripting_languages][http://en.wikipedia.org/wiki/Category:Statically_typed_programming_languages Statically_typed_programming_languages] | ||
Latest revision as of 16:21, 16 February 2015
Hack
Topics for Writing Assignment 1a, Spring 2015
Hack (programming language) Hack is a 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.
The goal of Hack is to offer developers a way to write cleaner, safer and refactorable code while trying to maintain a level of compatibility with current PHP codebases. The primary way that this goal is achieved is to provide developers a way to annotate PHP functions and classes with type information, providing a type checking tool to validate those annotations.
History
Hack was introduced by Facebook on March 20, 2014.
Motivation (Why Hack is created)
Every PHP programmer is familiar with day-to-day tasks that can be tricky or cumbersome. The code is a great example of a common mistake where a method could unexpectedly be called on a null object, causing an error that wouldn't be caught until runtime. Another example is a complex API, where developers may have a solid understanding of its semantics but still spend time looking up mundane method names in documentation.
At Facebook scale — with thousands of engineers shipping new code twice a day — slowdowns like these are even more problematic. Before Hack, we had a simple language with a quick feedback loop — but how could we mitigate the sorts of problems described above? Could early error detection coexist with rapid iteration, all while preserving our investment in PHP? Could improved code analysis and introspection help make developers more productive with tools like auto-complete?
Traditionally, dynamically typed languages allow for rapid development but sacrifice the ability to catch errors early and introspect code quickly, particularly on larger codebases. Conversely, statically typed languages provide more of a safety net, but often at the cost of quick iteration. We believed there had to be a sweet spot.<ref> Facebook, "Hack: a new programming language for HHVM" 2014-03-23</ref>
Background
Facebook engineers Bryan O’Sullivan, Julien Verlaguet, and Alok Menghrajani decided to build a programming language unlike any other.
Working alongside a handful of others inside the social networking giant, they fashioned a language that lets programmers build complex websites and other software at great speed while still ensuring that their software code is precisely organized and relatively free of flaws — a combination that few of today’s languages even approach. In typical Facebook fashion, the new language is called Hack, and it already drives almost all of the company’s website — a site that serves more than 1.2 billion people across the globe.
“We can say with complete assurance that this has been as battle-tested as it can possibly be,” says O’Sullivan, a veteran of iconic tech companies Sun Microsystems and Linden Lab who has long played an important role in a popular language called Haskell.
O’Sullivan and company publicly revealed their new language this morning, and at the same time, they “open sourced” it, sharing the technology with the world at large and encouraging others not only to use it, but to help improve it.
The software world is littered with programming languages, and new ones appear all the time. But according to some who have used it or who know the past work of those who built it, Hack has a design and a pedigree that immediately set it apart. “If Bryan O’Sullivan built it,” says programming guru David Pollak, who only yesterday heard about the new language, “I would walk across hot coals to use it.”<ref> CADE METZ, "Facebook Introduces ‘Hack,’ the Programming Language of the Future" 03.20.14</ref>
Features
Hack interoperates seamlessly with PHP, which is a widely used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. A majority of the valid PHP scripts is also valid in Hack; however, numerous less frequently used PHP features and language constructs are not supported in Hack.<ref> 1997-2015 the PHP Documentation Group, 2014-2015 Facebook "Hack Manual section- Hack and HHVM: Unsupported PHP Features in Hack" 2014-04-02</ref>
Hack extends the type hinting available in PHP 5 through the introduction of static typing, by adding new type hints (for example, for scalar types such as integer or string), as well as by extending the use of type hints (for example, for class properties or function return values). However, types of local variables cannot be specified. Since Hack uses a gradual typing system, in the default mode, type annotations are not mandatory even in places they cannot be inferred; the type system will assume the author is correct and admit the code.<ref> 1997-2015 the PHP Documentation Group, 2014-2015 Facebook "Hack Manual section-Partial Mode" 2015-02-02</ref> However, a "strict" mode is available which requires such annotations, and thus enforces fully sound code.<ref> 1997-2015 the PHP Documentation Group, 2014-2015 Facebook "Hack Manual section-Strict Mode" 2015-02-02</ref>
In addition,Hack provides the following, non-exhaustive list of features:
- Ability to annotate function and method return types.
- Ability to annotate the type of member variables.
- Protection against common unsafe coding patterns (e.g. sketchy null checks).
- Type-safe collections (Vector, Map, Set).
- A tool to check the types (the Hack type checker).
Hack has been designed with PHP compatibility in mind. The type checker enforces checks on typed code and assumes calls to/from PHP and other untyped code are correct. This means the amount of type checking will gradually increase as more and more code is written in Hack.
Hack is unlike many other statically typed languages:
- The type checking phase is instant.
- Local types are inferred.
- There is a minimal need for casting.
- The type related error messages are designed to make it easy to fix the core issue.
- The type checking model is a best effort model. Hack code can call PHP code and vice versa.
- Hack comes with the following benefits (and possible benefits):
- Return types makes the code more readable.
- Some typing mistakes will be caught by the type checker.
- Better emacs/vim/etc. integration.
- Future: Reliable refactoring tools.
- Future: Most typing mistakes will be caught statically.
Comparison between Hack and PHP
Hack is a language for HHVM. Introduction of HHVM makes PHP's fast performance many times faster. Moving your PHP code from PHP5 to HHVM is likely to result in a significant speedup.But the execution speed is the point of Hack -- the main point is about developer efficiency. Going from PHP on HHVM to Hack on HHVM, the gain is developer productivity<ref>Josh Watzman,"Is code written in Hack faster than code written in PHP on HHVM?" 2014-09</ref>
One can also say that Hack is like a “PHP++”. It adds optional static typing and generics to PHP. But at the same time, it doesn't have ubiquity, maturity, stability like PHP.<ref>Marco Arment "HACK isn't PHP 2014-3</ref>
Syntax and semantics
A Hack file always starts with <?hh. This is very similar to PHP. It has just replaced <?php with <?hh. Here's a sample code that demonstrates this.
<?hh echo 'Welcome to NC State';
The above code prints the following output:
Welcome to NC State
Functions
Hack functions are annotated with types. The example below explains this.
<?hh function add_one(int $x): int { return $x+1; }
Permitting Null Values
Prefixing a type with '?' permits null in Hack. Take a look at the example.
<?hh function f(?int $x): void { var_dump($x); } function test(): void { f(123); f(null); }
Handling Null Values
There are many ways to handle null values in Hack. One of them is throwing an exception. See the sample code.
<?hh interface User { public function getName(): string; } function get_user_name(?User $user): string { if($user === null) { throw new RuntimeException('Invalid user name'); } return $user->getName(); }
New Collection Types
Hack introduces new collection types: Vector, Set and Map. One is mentioned here.
Vector
Vector is preferred over array.
<?hh function test(): int { $vector = Vector {1, 2, 3}; $sum = 0; foreach ($vector as $val) { $sum += $val; } return $sum; }
Classes in Hack
It is really simple. See the class defined below:
<?hh class Point { private float $x; private float $y; public function __construct(float $x, float $y) { $this->x = $x; $this->y = $y; } }
All the members of a class must be initialized. There is also a shorter way to do the same:
<?hh class Point { public function __construct( private float $x, private float $y ) {} }
All the code snippets are referred from the source:<ref>Facebook "Hack tutorial" 2005</ref>
References
<references/>
External links
- Template:Official_website
- Hack language reference
- Facebook Q&A: Hack brings static typing to PHP world
- Full presentation on "Hack Dev Day"
Category:Dynamically typed programming languagesFacebookPHP_programming_languageScripting_languagesStatically_typed_programming_languages