CSC/ECE 517 Spring 2015/ch1a 20 HA: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 51: Line 51:


==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 [[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 name="unsupported">{{cite web
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 name="unsupported">{{cite web
  | url = http://docs.hhvm.com/manual/en/hack.unsupported.php
  | url = http://docs.hhvm.com/manual/en/hack.unsupported.php
  | title = Hack Manual | section = Hack and HHVM: Unsupported PHP Features in Hack
  | title = Hack Manual | section = Hack and HHVM: Unsupported PHP Features in Hack

Revision as of 17:23, 7 February 2015

Template:Infobox programming language

Hack is a language for HHVM that interoperates seamlessly with PHP. The barrier to entry for Hack is low. To get started, all that is needed is to generally understand the features that Hack provides and how to call the Hack type checker (hh_client invokes the type checker at the command line)

The goal of Hack 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.<ref></ref>

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.[1]

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.”[2]


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 name="unsupported"></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.<ref name="hack.annotations" /><ref name="hack.otherrulesandfeatures.typeinference" /> 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></ref> However, a "strict" mode is available which requires such annotations, and thus enforces fully sound code.<ref></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.

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.

function add_one(int $x): int {
  return $x+1;
}

See also

Template:Portal

References

External links

Template:PHP Template:Facebook navbox