r/programming • • Mar 20 '14

Facebook introduces Hack: a new programming language for HHVM

https://code.facebook.com/posts/264544830379293/hack-a-new-programming-language-for-hhvm/
802 Upvotes

526 comments sorted by

View all comments

Show parent comments

86

u/jvwatzman Mar 20 '14

Engineer working on Hack here.

For as much flak as PHP gets, there are actually a lot of good things about the language. The fast development cycle -- edit php script, refresh -- is something amazing that you don't get in a lot of statically typed languages, which usually have a compilation step. The crazy dynamic things you can do also occasionally have their place, though it's certainly easy to shoot yourself in the foot.

On the other hand, a lot of the time you want the safety that strong static typing can give you. Even just the null propagation checking can immediately find tons and tons of silly little bugs without even running the code, and ensure that the code stays consistent as a "mini unit test" if you will.

Hack hits the sweet spot of both. Wiring the Hack typechecker into vim was really revolutionary for me -- having both the immediate feedback of the type system for all the silly bugs that I was writing, along with the fast reload/test cycle from PHP, is great.

25

u/Eirenarch Mar 20 '14

Why is this considered so special? ASP.NET can do this since day one* and C# is much more statically typed and compiled than Hack. Hell, you can even edit the code while you have stopped at a breakpoint and continue.

*In case someone is wondering you need to use the Web Site project and not the Web Application one for actual save/refresh without compile behavior.

→ More replies (5)

27

u/detroitmatt Mar 20 '14

As a follow-up, I haven't had time to look over Hack's doc very comprehensively yet. In my opinion, a lot of the problem with PHP is its standard library: The language itself has a lot of neat features that would be dangerous if abused, and the stdlib abuses them, which is the problem, but if used responsibly are powerful, flexible, and useful. Therefore in as much as the standard library is the problem with PHP, does Hack's standard library avoid these problems?

0

u/[deleted] Mar 20 '14

[deleted]

→ More replies (8)

10

u/Error401 Mar 20 '14

What do you mean? Hack still keeps direct compatibility (in terms of interface) with standard PHP library functions. There are some things they left out intentionally that are overall problematic or the source of way too many bugs, so there's that.

16

u/detroitmatt Mar 20 '14

I suppose what I meant is "Will Hack have its own standard library that solves some the problems of PHP's?"

→ More replies (6)

7

u/argh523 Mar 20 '14

Funny..

  • if/then/else without {}

  • elseif (without the space between else and if)

That implies that you'd now have to write:

if ( $x > 0) {
    // foo
} else {
    if ( $x < 100 ) {
        // bar
    }
}

I doubt that's the case. But what I wrote above is what actually makes sense, and the elseif keyword would fix it. So now "else if" is just a two-word keyword, or "else without {}" isn't actually true.

But hey, it's a language based on PHP that is called Hack, so.. ;)

16

u/alokmenghrajani Mar 20 '14
if (...) {
  ...
} else if (...) {
  ...
} else {
  ...
}

is allowed. We should update our docs to clarify it.

→ More replies (1)

3

u/gclaudiu Mar 20 '14

if ( $x > 0) { // foo } else { if ( $x < 100 ) { // bar } }

Uh, you do realize you can do

if (condition) { // code } else if (other_condition) { // more code }

right?

2

u/argh523 Mar 20 '14 edited Mar 20 '14

I don't know, but yeah, I'm pretty shure that way of writing it is still supported in Hack. But the whole point of my comment is that it means you're using "else without {}", which they said is not supported in Hack.

→ More replies (6)
→ More replies (1)

6

u/NULLACCOUNT Mar 20 '14

Sorry if this is a dumb question, but why were references removed? Doesn't that limit a lot of functionality (or is there another way to pass-by-reference, etc)?

18

u/dparoski Mar 20 '14

Engineer working on Hack here.

Building on the doc that Error401 linked to (http://docs.hhvm.com/manual/en/hack.annotations.passingbyreference.php), when looking at how PHP references ("&") were used we found that in the overwhelming majority of cases references were used to pass a PHP array to a callee in manner that allowed the callee to mutate the original array (instead of mutating a copy of the array).

Unlike PHP arrays which have value-type semantics, Hack Collections were designed to have reference-type semantics (matching how all other objects behave in PHP 5 and above). There were multiple reasons for this design choice, but one of the main reasons was to make the use of references ("&") largely unnecessary for codebases written in Hack.

→ More replies (2)

9

u/alokmenghrajani Mar 20 '14

If you are using references to fetch data asynchronously, the async functions are a better replacement: your code will be type checkable and more readable.

Keep in mind that objects continue to be "passed by reference", so for all other use cases, you can always wrap your reference in a container. I.e. write a Ref<T> class and pass it around instead of using &.

→ More replies (1)

3

u/[deleted] Mar 20 '14

The crazy dynamic things you can do also occasionally have their place, though it's certainly easy to shoot yourself in the foot.

Can you give an example of some time you'd want to use a dynamic language over a statically typed language?

(I'd argue that there is usually a way to do the dynamic behavior in static languages, by using variable constructors and disjoint unions, but I think your argument is that there are times it's nice for that behavior to be the default.)

7

u/alokmenghrajani Mar 20 '14 edited Mar 20 '14

I can think of quite a few:

  • you are experimenting with an idea. You might want to tell the type checker to leave you alone while still be able to see your code run.

  • you are refactoring some large piece of code. You want to run tests or specific pieces of code to make sure you are headed in the right direction.

  • A part of your code might deal with lots of unstructured data (e.g. json) and it might be easier to write the code without boxing all the data in what boils down to untyped containers.

5

u/smog_alado Mar 20 '14

The partially-incorrect code is not necessarily an advantage of dynamic typing. Some languages, like Haskell, let you defer compile-time errors to runtime, allowing you to run partial code. Sure, dynamic languages do this "for free" but its no different from other type system features that they give "for free", like polymorphism and generics.

4

u/smog_alado Mar 20 '14 edited Mar 21 '14

If you insist on a "static checking for everything" route you can end up with really complex abstractions abstractions that are not worth your trouble (if you have ever seen a dependently typed language you know what I am talking about). Sometimes you are going to be better of having a simpler program that verifies things at runtime. For example, here are some things that are easier in a dynamic setting

  • Checking if array indices are out of bounds

  • Reflection and other kinds of introspection

  • JSON. In a static language you need to do boilerplate wraping/unwraping for many operations

0

u/[deleted] Mar 21 '14

[deleted]

5

u/[deleted] Mar 21 '14

[removed] — view removed comment

0

u/[deleted] Mar 21 '14

[deleted]

2

u/[deleted] Mar 21 '14

Or use spring-loaded or http://www.hotswapagent.org/. Both are free but not as featured as jRebel

2

u/benjumanji Mar 21 '14

Syntastic + Ghc-mod gives you all of that when editing Haskell. C# and Java both have ide's with incremental type checking. No build required. Haskell and ocaml have a repls. C# and Java have had the ability to evaluate expressions and change values on-the-fly during execution for years. What static languages are you referring to? I think it's awesome what you guys are doing for the php ecosystem, I just don't think from what I've seen that hack brings anything new to the table for someone not working with php.

2

u/UloPe Mar 22 '14

For as much flak as PHP gets, there are actually a lot of good things about the language.

Name one. (And "everybody already knows it" doesn't count)

3

u/[deleted] Mar 21 '14

How does hack compare to Python?

→ More replies (20)