Slashdot Mirror


Facebook Introduces Hack: Statically Typed PHP

alokmenghrajani (2602135) writes with news of Facebook's new Open Source language, Hack. Quoting: "Today we're releasing Hack, a programming language we developed for HHVM that interoperates seamlessly with PHP. Hack reconciles the fast development cycle of PHP with the discipline provided by static typing, while adding many features commonly found in other modern programming languages. ... 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. ... Hack has deep roots in PHP. In fact, most PHP files are already valid Hack files. ... Our principal addition is static typing. We have developed a system to annotate function signatures and class members with type information; our type checking algorithm infers the rest. Type checking is incremental, such that even within a single file some code can be converted to Hack while the rest remains dynamically typed. ... If a function parameter or class member does not have an explicit type annotation, the type checker considers its type to be dynamic, and it does not check the type of that value." In addition to static typing, they've introduced proper closures that capture the lexical environment, generics, collections, and array shapes. The Hack website has more details. There's a fairly complete language manual, tools to infer types in PHP source and annotate the code, and source available under the PHP license.

18 of 230 comments (clear)

  1. So many bugs by ClayDowling · · Score: 5, Insightful

    So many of the bugs that have tripped me up over the years would have been solved by simply having static typing.

    1. Re:So many bugs by MightyMartian · · Score: 5, Insightful

      It allows you to free your mind, express your inner coding monkey artist.

      And then you get to debug...

      --
      The world's burning. Moped Jesus spotted on I50. Details at 11.
  2. hate the name by mr_mischief · · Score: 5, Insightful

    "Hack" as a language name? Really?

    People are going to explain this at dinner parties. People who kind of understand that programming is more than being good at operating a computer as an end user but don't really know the difference between sysadmin, devops, programmer, business analyst, and DBA let alone what those roles really do are going to ask questions. Those questions will be things like "what kind of programming?", "what technologies do you use?", and "what are you working on right now?" The answer will be something about putting together a quick Hack program to change values in a database, and then it gets awkward.

    Plus, did they consider at all how easily this will get confused with Haxe?

    1. Re:hate the name by swb · · Score: 5, Funny

      "The answer will be something about putting together a quick Hack program to change values in a database"

      I take it you don't get invited back to dinner very often.

    2. Re:hate the name by pushing-robot · · Score: 5, Funny

      Hey, three focus groups chose this over "Kludge".

      --
      How can I believe you when you tell me what I don't want to hear?
    3. Re:hate the name by WillgasM · · Score: 4, Insightful

      It's actually very clever marketing. Now everybody that searches "facebook hack" will get this as the first result. They get lots of hits for their new language and obfuscate any articles teaching people how to exploit FB accounts. Win-win.

  3. Sarcastically Typed by wisnoskij · · Score: 5, Funny

    We really need a sarcastically typed language. That would be truly awesome.

    --
    Troll is not a replacement for I disagree.
  4. Design by contract? by afidel · · Score: 5, Insightful

    This sounds a bit like layering design by contract on top of a typically dynamically typed language rather than being a strictly statically typed language. It's an interesting approach and would seem to achieve their goals of faster but more robust development.

    --
    There are 4 boxes to use in the defense of liberty: soap, ballot, jury, ammo. Use in that order. Starting now.
  5. Re:English? by Anonymous Coward · · Score: 4, Funny

    Can someone please convert the summary in english, with basic explanations?

    My interpretation of the summary is: Facebook has sunk a lot of investment into a turd, and now they've determined that that turd needs polishing.

  6. Nope by Sarten-X · · Score: 4, Insightful

    In fact, most PHP files are already valid Hack files.

    No, no, no, and no.

    The single biggest problem with PHP is the tendency for old code and old programmers to keep their bad habits around when moving to new projects. PHP lacked vital modern features (like static typing and namespaces) for so long, and it's evolved so many workarounds (like magic quotes), that programmers have learned the wrong way to accomplish basic tasks. Now they have a new language, supporting the right way to do these things... but the old and broken ways still work. Sure, there will be a few programmers that will use the new way and be thrilled about the good technique, but then time crunches will set in, and code reviews will be rushed (or nonexistent), and those old ways will creep in, bringing the bugs with them.

    Backwards-compatibility with a broken language is a great way to improve a new language's adoption, and a terrible way to build a new language's reputation.

    --
    You do not have a moral or legal right to do absolutely anything you want.
  7. Re:English? by MightyMartian · · Score: 5, Insightful

    My interpretation is that they've put a lot of work turning PHP into Java or C. Why go through all this effort when they could simply used one of the C-like strongly typed languages is beyond me. All that effort could have been put into creating a PHP-to-Java converter or something along those lines.

    But hey, it's Mark Zuckerberg's bazillion dollars. If he wants to tape testicles on a eunuch, be my guest.

    --
    The world's burning. Moped Jesus spotted on I50. Details at 11.
  8. Re:static typing is awesome by Anonymous Coward · · Score: 4, Informative

    Let me introduce you to https://en.wikipedia.org/wiki/Cython

    You are welcome.

  9. Re:English? by alokmenghrajani · · Score: 5, Interesting

    Hack is more than turning PHP into Java or C. Java's type system is way different than C's (there are no objects in C). Hack's is different than Java's because it uses a type inference system. It also type checks incrementally, which allows the quick edit/save/reload cycles web developers are used to.

  10. Re:English? by Chris+Graham · · Score: 5, Informative

    For pragmatic-minded people, PHP is an extremely productive language to work in. No compiling, or waiting for compiling, no object files to mess with or get out of sync, and still relatively good speed. It really removes a lot of inconvenience between the programmer writing code and testing it. Despite what the detractors say, I've always found it a joy to work in (and I have worked in many languages).

    The down-side has always been that the language also had many sloppy characteristics. Modern PHP has cleaned up a lot of the language design errors, and static typing is extremely useful for reducing quantity of bugs (I know this as I developed a PHP fork that enforces this and it's saved me many many times over the years). HHVM adds much more speed, so that really complex modern social webapps are really needing. There are still many small inconsistencies in the language, but it's not a big deal given the other advantages.

  11. Re:English? by MightyMartian · · Score: 4, Interesting

    PHP is productive in that it can work as a fast prototyping language. Even I recognize that, and in fact use it as such.

    Where it falls down is that it doesn't enforce the kind of discipline that better languages do. It is well and truly the BASIC of the 21st century.

    That's not to say you cannot write good code in PHP. I do strive when I have to work in PHP (which is more than I would like) to write well-formed code, use newer language structures and so forth. But still, and maybe it's because I like the crutch, I just feel better coding in statically typed languages, and to be pretty blunt, if PHP is evolving into that kind of language, there are far better languages out there.

    --
    The world's burning. Moped Jesus spotted on I50. Details at 11.
  12. Re:English? by RabidReindeer · · Score: 4, Funny

    So not much different than PHP then...

    (ducks)

    It generates Perl?

  13. Re:English? by Chris+Graham · · Score: 5, Insightful

    Ah, it seems I started a religious language war simply by sticking up for the tool I (have to) use ;-). I've not claimed PHP is the best language, only it is a comfortable language to me and many others. I just do not feel the pain some developer's always claim to feel, the inconsistencies just don't cause ongoing real world problems for me. I do not lose productivity on a day-to-day basis, they do not cause additional bugs (well, dynamic typing could - and a small number of other things could if you don't have a framework / know the issues), they do not make the code unreadable, and I do not have a problem hiring or training competent developers in the language. Whatever problems there are are easily resolved, and the smallest problem I have as a professional. A PHP developer not aware of the few issues to avoid, would also not have the basic field knowledge to avoid a myriad of other pitfalls that affect all programming languages. I know so many people will read this and think "but there are issues, and you can't excuse that" - I'm not - but I also firmly believe going through your professional career with so much anality for the small niggles doesn't help anybody, and that instead of whinging, people should put their energy into something more productive and positive, like concentrating on client ROI (and no, the 5 minutes you took looking up an inconsistently named function the other day doesn't make an identifiable difference to it), or perhaps, putting in RFCs for cleaning up the issues for PHP6.

    The reason I develop in PHP is because I write consumer software to deploy on web hosts, and I don't chose the web hosts, and PHP is the overwhelming 'standard'. Probably the world would be better if Python replaced PHP, but bitching about the tools people use with arguments that the users of said tools simply cannot relate to, just seems ridiculous, and annoying because it just undermines the good work some seriously talented/hardworking/worthy people are doing. There is absolutely no reason PHP cannot continue to evolve, step by step, without forcing the entire hosting industry to adapt to something else (which is never going to happen).

    I just ported a project from ASP.net to PHP, and my God the problems I saw in that language. Whenever I look at Ruby code, I just see the most inelegant syntax - it just looks like it was deliberately designed to combine being cryptic in some places, with pulling in English words where self-describing layout would be better. And then there are a host of academic languages which have elegant concepts, but poor libraries, or just are too over-complex for real world use.

    For what it's worth, I think the world would be better off if Javascript was replaced too (I get annoyed by that much more than PHP actually, because some basic programming constructs are just hacks, and it is literally impossible to write elegant looking code).

  14. Re:English? by Pseudonym · · Score: 4, Informative

    No. Facebook is developing a new language which is syntactically similar to, and inter-operates with, PHP. Calling Hack a "version of PHP" is like calling Delphi a "version of ALGOL".

    --
    sub f{($f)=@_;print"$f(q{$f});";}f(q{sub f{($f)=@_;print"$f(q{$f});";}f});