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

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
{{distinguish|Haxe (programming language)}}
{{Infobox programming language
| name                = Hack
| name                = Hack
| logo                = File:Hack - Logo.png
| logo                = File:Hack - Logo.png

Revision as of 16:56, 7 February 2015

| name = Hack | logo = File:Hack - Logo.png | logo_size = 200px | logo_alt = Logo of Hack | released = 2014 | designer = Julien Verlaguet, Alok Menghrajani, Drew Paroski, and others<ref></ref> | developer = Facebook | typing = static, dynamic, weak | influenced_by = PHP, Java, C# | programming language = | platform = | operating_system = Cross-platform | license = BSD License<ref name="license"></ref> | website = Template:URL }}

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

Background

Hack was introduced by Facebook on March 20, 2014.<ref></ref>

Facebook engineers Bryan O’Sullivan, Julien Verlaguet, and Alok Menghrajani spent the last few years building 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.”

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>

Syntax and semantics

A Hack file always starts with <?hh. This is very similar to PHP. It has just replaced <?php with <?hh.

<?hh
echo 'Hello World';

The above script, similar to PHP, will be executed and the following output is sent to the browser:

Hello World

An important point to note is that unlike PHP, Hack and HTML code do not mix. Normally you can mix PHP and HTML code together in the same file, like this:

<html>
    <head>
        <title>PHP Test</title>
    </head>
    <body>
        <!-- hh and html do not mix -->
        <?php echo '<p>Hello World</p>'; ?> 
    </body>
</html>

This type of code is not supported by Hack; either XHP or a template engine need to be used.<ref name="unsupported" />

Functions

Hack allows types to be specified for function arguments and function return values. Functions in Hack are thus annotated with types like the following:

<?hh
// Hack functions are annotated with types.
function negate(bool $x): bool {
    return !$x;
}

See also

Template:Portal

References

External links

Template:PHP Template:Facebook navbox