7 ms·
I'm the manager of the team that developed Hack, and I'm sitting here with some of the language designers. Happy to answer your questions.
by bos 13y ago
I'm the manager of the team that developed Hack, and I'm sitting here with some of the language designers. Happy to answer your questions.
- Locke1689 13y agoPretty star-studded cast you have on the core team, there. What's your motivation, aside from modernizing Facebook's code base? What language niche does Hack serve which is not served by other languages? Why Facebook at all (aside from the rarity of finding a company to pay you to write a compiler :))?
- bos 13y agoThe sweet spot that Hack hits is that it combines gradual typing (an idea that hasn't yet seen much real-world adoption) with an incredibly fast typechecker. This lets you choose the pace and extent to which you want to adopt the safety of static typing, while preserving your dynamically typed code -- and without sacrificing the rapid turnaround of PHP. That's a unique combination, in my experience.
- Confusion 13y agoHow does your approach compare to Typed Racket and Typed Clojure? Could they conceivably achieve the same performance or is there a fundamental difference?
- webjprgm 13y agoDoes Typed Racket have DrRacket support for instant feedback on errors? (It didn't 4+ years ago when I used Racket.) Typed Racket also includes more types, like ": Integer [more precisely: Negative-Fixnum]" (from docs), as well as polymorphic data structures and higher-order functions. I don't know if this is a "fundamental difference" but it might mean Facebook's type checker can optimize in certain ways that Racket's cannot.
- samth 13y agoYes, Typed Racket does have that -- DrRacket continuously expands in the background, so Typed Racket gets it for free by integrating into the macro system. The Typed Racket type checker is much slower than Hack, though. Hack does seem to have polymorphic data structures and higher-order functions.
- thescrewdriver 13y agoWhy did you pick the name "Hack"?
- neotrinity 13y agobecause it is a HACK !!?!
- axman6 13y agoIt involves PHP so...
- deleted 13y ago[deleted]
- robin_reala 13y agoPresumably because it brings them some value?
- bos 13y agoWe wanted to preserve our investment in PHP, while introducing new capabilities and safety that make rapid development at scale less daunting.
- rarestblog 13y agoWhat IDE do people use for Hack?
- bos 13y agoStay tuned. You'll hear more in due course. In the mean time, the open source release of Hack includes integration with both Emacs and Vim.
- Phlarp 13y agoFacebook IDE incoming?
- lpw25 13y agoI think they mentioned something in the browser using js_of_ocaml at CUFP last year.
- jwatzman 13y agoThe js_of_ocaml bits are actually what are powering our interactive tutorial (http://hacklang.org/tutorial/ http://hacklang.org/tutorial/) -- the tutorial is actually a full build of the typechecker running in your browser :) The source for it is mostly at https://github.com/facebook/hhvm/tree/master/hphp/hack/src/js https://github.com/facebook/hhvm/tree/master/hphp/hack/src/j... minus a little bit of glue.
- jasonlotito 13y agoIf it involves Jets and brains..., then you will have won the interweb awards so soon in the year.
- henrikbjorn 13y agoWill the vim plugin be seperated out into a subsplit so it can be used with Vunde or any of the other package managers that exist for VIM?
- diegoperini 13y ago
- ecaron 13y agoDo you imagine a future where Hack will merge back into PHP (like PHP 7), in the same style that Beryl & Compiz then rejoined? Or does the team intend for the two to always be adjacent-yet-separate?
- Joeri 13y agoHhvm is not a fork of php. To my knowledge it's a radically different codebase that reimplements php's api.
- SaraMG 13y agoHHVM developer & PHP runtime developer here. I've got hands in both runtimes and all even I can say is: Maybe. I think the most likely outcome is that PHP will adopt some of HHVM's additional features, but remain a separate project. IMO that's a great outcome, since we'll both likely drive the other to be better.
- octo_t 13y agowhats the state of the type system like? Here[1] it suggests that it may have bugs, is this a 'covering backside' thing or is it currently unsound? [1] - http://hacklang.org/manual/en/hack.annotations.summary.php http://hacklang.org/manual/en/hack.annotations.summary.php
- jwatzman 13y agoEngineer working on Hack here. It's complicated. If you use partial mode, which doesn't enforce that every function is fully typed, then it's trivial to break the type system by just using an untyped function. In order to ease conversion, we just assume the programmer knows what they are doing with untyped functions and let anything pass. If all of your code is in strict mode, then we believe the type system to be sound. We haven't done any formal proof of this of course, and there have been plenty of bugs in the past. But that's the goal. Some types are enforced at runtime -- just like PHP5 enforces class type annotations on parameters. However, at least for now, the runtime doesn't do a lot of the clever things with the type system that the static typechecker does. It's much more conservative with using the type information, and doesn't do things like check generics at all. (At least right now! We probably will change this in the future.) This means that we can play a little more fast and loose with the type system in the static parts; we want it to be sound, but it doesn't have to be right now; it's not going to cause a JIT crash or anything. There are more details on the modes here: http://hacklang.org/manual/en/hack.modes.php http://hacklang.org/manual/en/hack.modes.php
- samth 13y ago> If all of your code is in strict mode, then we believe the type system to be sound. We haven't done any formal proof of this of course, and there have been plenty of bugs in the past. But that's the goal. When I look at this from the docs, it seems unsound: "Hack treats traits as a stand-alone entity during the type checking process. In other words, it ensures type consistency within the trait (i.e., as a black box, so to speak), but does not "copy and paste" the code into all of the classes that use the trait and check for type consistency there. The reason this is done comes down to performance." Is there something I'm missing that does make this sound?
- caiob 13y agoIs this just syntax sugar or there are performance improvements upon php?
- bos 13y agoWe are just beginning to use the static type information that Hack provides to yield performance optimizations, and that's definitely on the cards as something we'd like to push further.
- Joeri 13y agoStatic types are more than syntactic sugar, they allow you to make additional guarantees about the behavior of the code, like having unit tests without actually writing them. I program in php every day and miss static typing (as opt-in mechanism). Sadly i'm on windows and use oracle, so hhvm is not applicable.
- wiremine 13y agoThanks for taking the questions! How extensively is Facebook using Hack at this point? Is it in production? What has been the biggest learning/unlearning you've needed to do going from PHP to Hack?
- bos 13y ago100% of our web front end developers use Hack now. This has been an organic process of growth over the past year, by which I mean our engineers are using it because they like it and see value in it, not because there's someone standing over them with a big stick :-) The biggest learning step for our engineering teams was to treat type errors from Hack as actual logic errors. We have a collection of "linters" that provide advice on code style and other nice-to-have factors. Some people (quite reasonably) initially thought of Hack errors as lint-like stuff that it was safe to ignore, when in fact they indicate real logical inconsistencies in code.
- wiremine 13y ago> Some people (quite reasonably) initially thought of Hack errors as lint-like stuff that it was safe to ignore, when in fact they indicate real logical inconsistencies in code. Interesting, thanks! One followup: what has it been like from an ops perspective? Similar to PHP, or is there a better frame of reference?
- nbm 13y agoCan you rephrase your question about the ops perspective? Is there something in particular you want to know about?
- wiremine 13y agoSure. I guess I'm wondering how it compares to running plain old Apache/PHP in a production environment. Or, is it more like a Django/Rails stack? Does it use the same memory footprint as PHP, etc?
- VeejayRampay 13y agoCould the same techniques you applied to PHP be applied to other dynamic languages like Ruby or Python?
- bos 13y agoYes, definitely.
- jnbiche 13y agoIf you're looking for a statically-typed version of Python/Ruby, I'd say nimrod gets fairly close. It's also fantastically fast and compiles to portable ANSI C.
- sanxiyn 13y agoNimrod is not a statically typed version of Python in the sense Hack is a statically typed version of PHP. Not at all. Hack is gradually typed, Nimrod is not. Big difference.
- jnbiche 13y agoI thought it was clear I wasn't trying to draw an analogy between PHP/Hack and Python/Nimrod, but rather pointing out that Nimrod is a very nice language for someone who enjoys Python but wants static typing.
- programminggeek 13y agoIs there any interest in adding named parameters to Hack?
- TazeTSchnitzel 13y agoThat's coming in PHP 5.7 anyhow. It's been implemented and works, but Nikita Popov hasn't had the time to change the function definitions of the standard library to work with it, hence PHP 5.6 won't have it, sadly.
- programminggeek 13y agoWow, I haven't kept up with things, but gosh that sounds pretty great. Type checking + named params is a fairly difficult combination of great language features to find in the same language.
- wtetzner 13y agoI think both C# and Scala have both features.
- prototrout 13y agoAt a glance it looks like Hack is to PHP what TypeScript is to JavaScript. Is that a fair analogy?
- julienv 13y agoYes and no. Yes, because TypeScript is bringing a type-system to a dynamically typed language and so did Hack. No: because Hack is bringing some additional language features affecting the runtime. Modest changes for now, but we intend to carry on in that direction.
- jabits 13y agoWell, it seems Microsoft is doing similarly with Typescript, except instead, waiting for the spec to catch up. I like both ideas.
- Touche 13y agoI don't know what you mean by that... stuff like classes were already in the harmony proposal phase before Typescript implemented them. Stuff like type hints won't be in JS ever.
- michaelwww 13y agoAt least to date, Microsoft stresses the "TypeScript is just JavaScript." New language features are added to ES6 and then wrapped with types in TypeScript.
- ahoge 13y ago> No: because Hack is bringing some additional language features [...] TypeScript added classes, interfaces, modules, and arrow functions.
- ufo 13y agoThe key difference is affecting the runtime. All those typescript features can be compiled down to regular JS and typescript-generated javascript can be run in a browser without extending the JS engine.
- acorkery 13y agoI like the async/await additions. Seems very similar to the .NET implementation. Was this an inspiration?
- bos 13y agoYep, we're happy to be inspired by good ideas when they're obviously the right path to walk.
- tveita 13y agoMy impression is that Facebook mostly write their stand-alone services in Java or C++, and are using PHP only where they're "stuck" with it due to a large existing code base. Do you think Hack is a good language to start a new project in, compared to non-PHP languages? Are you using Hack for things besides the main web page?
- jwatzman 13y agoEngineer working on Hack here. Yeah, I think Hack is a good language to start a new project in. 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.
- copergi 13y ago>The fast development cycle -- edit php script, refresh -- is something amazing that you don't get in a lot of statically typed languages You seriously think that? That's how we do haskell web development. Both yesod and snap do this out of the box. That's how every java developer I know works.
- bos 13y agoI have a little bit by way of Haskell chops, and I'll venture that the performance of the Hack typechecker is a very big deal, and it is in a different breed than the turnaround time you get from snap or yesod (or Java).
- diegoperini 13y agoGenerics, lambdas, type annotations. This looks awesome, seems like the next generation for PHP programming. Can I embed it? Or extend it via compiled binaries written in C/C++?
- SaraMG 13y agoI'm working on proper documentation for extensions as we speak, but for now you can find some very basic info at https://github.com/facebook/hhvm/wiki/Extension-API https://github.com/facebook/hhvm/wiki/Extension-API . As the author of pretty much THE book on writing PHP Extensions ( http://amazon.com/dp/067232704X http://amazon.com/dp/067232704X ), I can say with a fair bit of authority that writing HHVM extensions is SO SO SO much easier.
- ampersandy 13y agoJust a heads up that the search page seems to be 404'ing.
- bos 13y agoOops! Thanks - we're fixing it now.
- mintplant 13y agoSay I'm starting out with an entirely new project and want to leave the legacy of dynamic typing behind. Is there a flag available to enforce the use of type signatures, causing Hack to throw a compilation error when they're omitted?
- anaphor 13y agoIt looks like you've done a good job with closures, type inference, etc... Would you consider adding even more expressive features like algebraic types or hygienic macros to the language? Also how likely do you think it is that typical PHP will eventually be overtaken by HHVM? I'm hoping it will!
- freyrs3 13y agoHi Bryan, I know most people know you from your prolific work on many great Haskell libraries ( Criterion, Attoparsec, Aeson, ...). Did Haskell have any role in the development of Hack? Looking at the code base it seems like the type system is primarily written in ML, what made the team decide to use OCaml over Haskell?
- bos 13y agoAs you note, the team developed the typechecker in OCaml, as that's what the founding engineers were familiar with. Many of ML's cousin languages happen to be well suited to this kind of work.
- more_original 13y agoHave you considered using some standard library replacement like Core?
- camus2 13y agoHi and thanks, Are you planning a Windows build ? do you think Hack could replace all PHP stacks (PHP cloud stacks , not talking about shared host wordpress toys) in a 5 years time frame ? What do you think about the current state of PHP? what would you tell PHP core devs about that? Does an async frameworks like React runs with Hack? What can you answer to people that are concerned that it is Facebook who is developping this?
- ptarjan 13y ago> Are you planning a Windows build ? Yes. auroraeosrose is working on it (the same person who did the port for php) https://github.com/auroraeosrose/hhvm/tree/win32_start https://github.com/auroraeosrose/hhvm/tree/win32_start
- vajrabum 13y agoDoes this presentation match the current version of Hack fairly well? It has a nice explanation of the type system that is more concise than the one on the web site. https://raw.github.com/strangeloop/StrangeLoop2013/master/slides/sessions/Adams-TakingPHPSeriously.pdf https://raw.github.com/strangeloop/StrangeLoop2013/master/sl... The idea of bolting a static checker onto a dynamic language and using an unresolved top type to make it work is cool. The presentation refers to your system as SoA gradual typing. Are there any papers or presentations which explain that approach and how it works in more detail? Particularly in how it might differ from gradual typing?
- wambotron 13y agoWhy use public string $x = ''; instead of public $x:string = ''; It seems inconsistent to me, probably because I've used AS3/Haxe.
- Rican7 13y agoIts not just inconsistent with other languages, its consistent with their own language. Property types are declared before the property name, while function return types are declared after (and with a colon). Very strange.
- fzltrp 13y agoIf you look at the original C syntax for function types, it is also a bit weird: part of the type (the return type) is placed before, and part (the parameters) after. That syntax in hack seems more "functional" to me. That said, I must admit that I kind of like the C syntax, because of its oddity.
- fauigerzigerk 13y agoBut inconsistency is consistent with PHP's philosophy ;-)
- kreek 13y agoI too programmed for a long time in AS3/Haxe, and while I prefer variable:type as I think it's more readable, most static languages (java, C#) do it the other way.
- wambotron 13y agoYeah, I'm just not sure why they have public function get_name(): string but use public string $x = ''; The incosistency drives me a bit crazy.
- wvenable 13y agoPHP is based on C. It's way more consistent with C, Java, C# , etc to do the former. So the real question is why function foobar() : int { .. } instead of function int foobar() { .. }
- maxiepoo 13y agoHi Bryan, How do you protect from type errors when a value crosses from the untyped to typed fragment? Do you use contracts?
- jwatzman 13y agoNot Bryan, but I am an engineer who works on Hack :) We don't (statically) protect against type errors when you cross from untyped into typed code. If you call an untyped function, we assume the programmer knows what they are doing -- just like PHP does. You might get a runtime exception if you are calling a method on null, for example. This is actually a pretty important part of the conversion process. You don't have to convert all your code all at once. Typed code can be verified statically; untyped code is assumed to work just as before. Parameter and return type annotations are enforced at runtime by HHVM (just like you can add a class type annotations to a parameter in PHP). This will protect against some inconsistencies -- again, just as if you weren't using Hack.
- maxiepoo 13y agoOh I see, so there's no change in code generation for typed code, then?
- jwatzman 13y agoAt runtime, we check and enforce parameter types at function entry and return types at function exit. The extra type information can also enable the JIT to emit more efficient code in some cases, and work is ongoing to make that even more efficient. But having these types is independent of being in Hack -- you can have fully untyped Hack code, though I wouldn't advise it, as you are leaving one of the most powerful features of the language on the table.
- maxiepoo 13y agoWhat do you do with higher-order functions (which PHP has I believe)? If I write a typed map : (a -> b) -> [a] -> [b] and then it's called with an untyped function as input, but it returns a something that's not a b. Do you place some kind of barrier around it so that that's stopped immediately?
- harharhir 13y agoThank you for your work on this and for open sourcing it. It looks like it can make large code bases in PHP a great deal more manageable. Do you expect Hack to be stable without large breaking changes going forward? The documentation doesn't say much about the scheduling for asynchronous tasks. Can async functions be used to batch requests to e.g. caches and databases? Can Awaitable be used to interface with code that expects chainable Future or Promise-style interfaces? Can HHVM/Hack use standard PHP extensions, or how much work is it to port an extension? Is there a Hack plugin for IntelliJ? Does the Hack project have a mailing list or forum?
- SaraMG 13y ago"Can HHVM use standard PHP extensions?" Our internal plumbing is different, so the extension APIs aren't the same, thus PHP extensions can't normally be used with HHVM. Paul has a source-compatability transformer which works for simple extensions (no fancy stuff allowed), but we recommend anyone with an extension consider porting the code. If your extension is one of those "This was written in PHP but we wanted it to be faster so we turned it into C" types, you should consider going back to PHP for it. In practice, we find that HHVM can JIT PHP code into running about as fast as (sometimes faster than) C/C++ extension code. If you do need to dip into C/C++ (because you're calling an external library), the API we've designed is a LOT easier to work with than PHP's. And I say that with the authority of having written THE book on writing PHP extensions: http://amazon.com/dp/067232704X http://amazon.com/dp/067232704X . I'm working on proper documentation for extensions as we speak, but for now you can find some very basic info at https://github.com/facebook/hhvm/wiki/Extension-API https://github.com/facebook/hhvm/wiki/Extension-API .
- fzltrp 13y agoGreat work! A question about the type inference mechanism: in PHP, while it is possible to define object interfaces which classes may be defined against, by the dynamic nature of the language, functions don't necessarily need that interface specification to accept an object conforming to it, explicitely or implicitely. OCaml provides something "similar" with its object system, but much more powerful with static inference of an object (super)type from its usage. will Hack be able to infer an object interface as well from its usage in a function?
- jwatzman 13y agoEngineer working on Hack here. We don't do any type inference across function boundaries, so we largely dodge the issue that I think you are getting at. (Please elaborate if I misunderstand!) We rely on interface and class definitions in order to know what methods are available, and even though the runtime resolves everything at runtime so you can call any method that happens to exist at that time ("duck typing"), we enforce statically that methods do exist where we can. So for example, the following code will work at runtime since the method `g` does exist, our static typechecker will reject it since it does not exist on type `I`. <?hh // strict interface I { public function f(): void; } class C implements I { public function f(): void {} public function g(): void {} } function f(I $i): void { $i->g(); } f(new C());
- fzltrp 13y agoThank you! You brought an interesting point with the example you gave, it is one of the things I was thinking about. Thank you for your work, and for making it available!
- julienv 13y agoThere seem to be 2 questions here: 1) Is Hack type inference total? Answer no: you must annotate parameters and return types. It would be pretty much impossible to implement total type-inference without loosing separate compilation in Hack. PHP projects are not organized around a module system, which means that you have "spaghetti" dependencies, and even better, cyclic dependencies all over the place. So trying to implement total type-inference would be a bad idea, you would not be able to separate the code in independent entities and the checker would not scale. 2) Does Hack support structural sub-typing? Answer: No, but not for obvious reasons. Fun fact, the first version of the type-checker was implementing structural sub-typing. And it was not scaling, for subtle reasons. Hack allows covariant return types, so if we implemented structural sub-typing we would have to "dive" into the return types of each method to see if they are compatible. But in turn, these objects could have covariant return types etc ... The process of checking that was too inefficient. Caching is a bad idea (or at least a non trivial idea to implement), because of constraints and type-variables. Since disallowing covariant return types was not an option (it was crucial to make a lot of code work), we had to kill structural sub-typing. I hope this answers your question. As a big OCaml fan myself, I like the features you just mentioned (Well, Hack is written in OCaml), but they really didn't seem to be a good fit due to the nature of the language and the kind of checking speed we were shooting for.
- valarauca1 13y agoWith Async functions I'm not sure if the documentation is lacking or I'm just slightly thick, I believe the later. (I'm not a php developer so excuse my ignorance) It seems you only have 1 method 'await', to check if the async function has completed its job (it self, if I'm understanding is a blocking operation in which ever thread/worker its called). So is there a way to run something more similar to $i = 1; while(doingSomeThing) { $b = ""; if(completed gen_foo()) { $b += await gen_foo(); do_critical_task($b); } else { do_something(do_item[i]); } $i++; } Because as I see it now, what ever I run asynchronously I.E.: gen_foo(get_user_data); //async $x = do_something_for_a_while(); //do something while gen foo processes do_new_thing($x, await gen_foo()); //do something with rendezvous result I'm forced to time out my async calls so that they'll rendezvous in the same place won't I? Again if I'm completely off the mark please let me know.
- elgenie 13y agoasync/await allows cooperative multitasking on a single thread: stay tuned for more... The way it works is quite different from your example: async function gen_foo(): Awaitable<string> { echo "until we get to an await, eager execution\n"; // ... await gen_bar(); // this code is only executed after await return 'result'; } $x = gen_foo(); // x is a handle, suspended at the point of its first 'await' await $x; // ... and now it's resumed The benefit comes from being able to batch these async functions together: list($x, $y, $z) = await genva( gen_foo(), gen_bar(), gen_baz(); ); // genva creates a wait handle for awaiting its args // ... and when we get here $x, $y, and $z are all assigned
- zzaman 13y agoI'm a little confused as to the need for Hack unless you have a code base in PHP and need to ship tighter code (which is a problem Facebook has and my team probably has as well). Adding lambdas makes PHP more Ruby-like and generics and type checking are straight out of Java. I'm still unconvinced in how this makes programming websites more efficient or bug-free than existing languages. Can you please elaborate on that?
- lpw25 13y agoI rather think lambdas, generics and type inference are straight out of ML, especially as hack is written in OCaml.
- antihero 13y agoWhy on earth would the solitary example for a shiny new language, use the awful mysql_ library for PHP instead of PDO?
- jhspaybar 13y agoHave you guys looked at the new Truffle/Graal back end in Java 8? It has seen some impressive numbers in other languages, did you explore this as a possibility?
- donutdan4114 13y agoAny plans to have overrides for return types in the same class? I think that would be amazing. http://docs.hhvm.com/manual/en/hack.otherrulesandfeatures.overridereturntype.php http://docs.hhvm.com/manual/en/hack.otherrulesandfeatures.ov...