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

From Expertiza_Wiki
Jump to navigation Jump to search
(Blanked the page)
No edit summary
Line 1: Line 1:
{{distinguish|Haxe (programming language)}}
{{Infobox programming language
| 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>{{cite web|url=http://www.serpentine.com/blog/2014/03/28/where-credit-belongs-for-hack/ |publisher=Bryan O'Sullivan |title=Where Credit Belongs for Hack |date=2014-03-28 |accessdate=2015-02-02}}</ref>
| developer            = [[Facebook]]
| typing              = [[Static typing|static]], [[Dynamic typing|dynamic]], [[Weak typing|weak]]
| influenced_by        = [[PHP]], [[Java (programming language)|Java]], [[C Sharp (programming language)|C#]]
| programming language =
| platform            =
| operating_system    = [[Cross-platform]]
| license              = [[BSD License]]<ref name="license">{{cite web
| url = https://github.com/facebook/hhvm/blob/master/hphp/hack/LICENSE
| title = facebook/hhvm | section = hhvm / hphp / hack / LICENSE
| date = 2014-03-20 | accessdate = 2014-08-11
| website = github.com | publisher = [[Facebook]]
}}</ref>
| website              = {{URL|http://hacklang.org/}}
}}


'''Hack''' is a [[programming language]] for the [[HipHop Virtual Machine]] (HHVM), created by [[Facebook]]. The language is [[open source]], licensed under the [[BSD License]].<ref name="license" /><ref name="oreilly">{{cite web
| url = http://radar.oreilly.com/2014/04/facebooks-hack-hhvm-and-the-future-of-php.html
| title = Facebook’s Hack, HHVM, and the future of PHP
| date = 2014-04-03 | accessdate = 2014-08-02
| author = Josh Lockhart | publisher = [[O'Reilly Media]]
}}</ref><ref>{{cite web
| url = http://www.wired.com/wiredenterprise/2014/03/facebook-hack/
| title = Facebook Introduces 'Hack,' the Programming Language of the Future
| date = 2014-03-20 | accessdate = 2014-04-15
| author = Cade Metz | publisher = [[Wired (website)|Wired]]
}}</ref>
Hack is a dialect [[PHP]] that also runs on HHVM, but it allows programmers to use both [[dynamic typing]] and [[static typing]].  This kind of a [[Type systems|type system]] is called [[gradual typing]], which is also implemented in other programming languages such as [[ActionScript]].<ref>{{cite web
| url = https://www.cs.umd.edu/~avik/papers/iogti.pdf
| title = The Ins and Outs of Gradual Type Inference
| date = January 2012 | accessdate = 2014-09-23
| author1 = Aseem Rastogi | author2 = Avik Chaudhuri | author3 = Basil Hosmer
| publisher = [[Association for Computing Machinery]] (ACM) | format = PDF
}}</ref>  Hack's type system allows types to be specified for [[Function (programming)|function]] [[Argument (computer programming)|arguments]], function [[return value]]s, and [[class properties]]; however, types of [[local variable]]s cannot be specified.<ref name="oreilly" /><ref name="hack.annotations">{{cite web
| url = http://docs.hhvm.com/manual/en/hack.annotations.php
| title = Hack Manual | section = Hack and HHVM: Type Annotations
| accessdate = 2014-03-25
| website = docs.hhvm.com
}}</ref><ref name="hack.otherrulesandfeatures.typeinference">{{cite web
| url = http://docs.hhvm.com/manual/en/hack.otherrulesandfeatures.typeinference.php
| title = Hack Manual | section = Hack and HHVM: Type Inference
| accessdate = 2014-03-25
| website = docs.hhvm.com
}}</ref>
==History==
Hack was introduced on March 20, 2014.<ref>{{cite web|url=https://code.facebook.com/posts/264544830379293/hack-a-new-programming-language-for-hhvm/ |title=Hack: a new programming language for HHVM |website=code.facebook.com |publisher=[[Facebook]] |date= |accessdate=2014-03-23}}</ref> Before the announcement of the new programming language, Facebook had already implemented the code and "battle tested" it on a large portion of its web site.
==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
| url = http://docs.hhvm.com/manual/en/hack.unsupported.php
| title = Hack Manual | section = Hack and HHVM: Unsupported PHP Features in Hack
| accessdate = 2014-04-02
| website = docs.hhvm.com
}}</ref>
Hack extends the [[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>{{cite web | url = http://docs.hhvm.com/manual/en/hack.modes.partial.php | title = Hack Manual | section = Partial Mode | accessdate = 2015-02-02 | website = docs.hhvm.com}}</ref> However, a "strict" mode is available which requires such annotations, and thus enforces fully sound code.<ref>{{cite web | url = http://docs.hhvm.com/manual/en/hack.modes.strict.php | title = Hack Manual | section = Strict Mode | accessdate = 2015-02-02 | website = docs.hhvm.com}}</ref>
== Syntax and semantics ==
The basic file structure of a Hack script is similar to a PHP script with a few changes. A Hack file starts with <tt><?hh</tt> as opposed to <tt><?php</tt> for a PHP script:
<syntaxhighlight lang="php">
<?hh
echo 'Hello World';
</syntaxhighlight>
The above script, similar to PHP, will be executed and the following output is sent to the browser:
<syntaxhighlight lang="html4strict">
Hello World
</syntaxhighlight>
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:
<syntaxhighlight lang="php">
<html>
    <head>
        <title>PHP Test</title>
    </head>
    <body>
        <!-- hh and html do not mix -->
        <?php echo '<p>Hello World</p>'; ?>
    </body>
</html>
</syntaxhighlight>
This type of code is not supported by Hack; either [[XHP]] or a [[Web template system|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:
<syntaxhighlight lang="php">
<?hh
// Hack functions are annotated with types.
function negate(bool $x): bool {
    return !$x;
}
</syntaxhighlight>
== See also ==
{{Portal|Computer programming}}
* [[KPHP]]
* [[Parrot virtual machine]]
* [[Phalanger (compiler)|Phalanger]]
* [[Project Zero]]
== References ==
{{Reflist|30em}}
== External links ==
* {{Official website|http://hacklang.org}}
* [http://hacklang.org/tutorial/ Official tutorial]
* [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://hacklang.org Full presentation on "Hack Dev Day"]
{{PHP}}
{{Facebook navbox}}
[[Category:Dynamically typed programming languages]]
[[Category:Facebook]]
[[Category:PHP programming language]]
[[Category:Scripting languages]]
[[Category:Statically typed programming languages]]

Revision as of 16:19, 7 February 2015

Template:Distinguish Template:Infobox programming language

Hack is a programming language for the HipHop Virtual Machine (HHVM), created by Facebook. The language is open source, licensed under the BSD License.<ref name="license" /><ref name="oreilly"></ref><ref></ref>

Hack is a dialect PHP that also runs on HHVM, but it allows programmers to use both dynamic typing and static typing. This kind of a type system is called gradual typing, which is also implemented in other programming languages such as ActionScript.<ref></ref> Hack's type system allows types to be specified for function arguments, function return values, and class properties; however, types of local variables cannot be specified.<ref name="oreilly" /><ref name="hack.annotations"></ref><ref name="hack.otherrulesandfeatures.typeinference"></ref>

History

Hack was introduced on March 20, 2014.<ref></ref> Before the announcement of the new programming language, Facebook had already implemented the code and "battle tested" it on a large portion of its web site.

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

The basic file structure of a Hack script is similar to a PHP script with a few changes. A Hack file starts with <?hh as opposed to <?php for a PHP script:

<?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