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

From Expertiza_Wiki
Jump to navigation Jump to search
Line 52: Line 52:
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&nbsp;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> 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>
Hack extends the [http://en.wikipedia.org/w/index.php?title=Type_hinting&redirect=no type hinting] available in PHP&nbsp;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:

Revision as of 18:04, 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.

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.

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
  ) {}
}

Source:<ref>Facebook Hack tutorial 2005</ref>

See also

Template:Portal

References

<references/>

External links

Template:PHP Template:Facebook navbox