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.

41 of 230 comments (clear)

  1. Spy by Sigvatr · · Score: 2

    Obviously a backdoor for the NSA 8)

  2. Re:English? by Anonymous Coward · · Score: 2, Informative

    FaceBook is developing an in-house version of PHP that adds optional static typing and some other features.

  3. 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 Anonymous Coward · · Score: 3, Insightful

      Dynamic typing frees your mind to think about the problem at hand and the best solution to that problem, rapidly changing approach and methods as you learn from experience gained. I get sick of spending half my time dicking around with static types, casts, etc and spinning my wheels chasing down type errors when I could be progressing rapidly towards a solution. Unless I'm writing kernel code for a drive by wire system or something similarly life-and-death, I'll take dynamic typing any day and be done twice as fast, moving on to the next challenge. If absolutely necessary, once a final working codebase is established, it can be rewritten to take advantage of the additional lock-down offered by static types.

    2. Re:So many bugs by CastrTroy · · Score: 2

      Yeah, with static typing, there's a whole class of errors that simply can't happen. Any time you lose from having to explicitly declare variable types, or casting variables to the correct time is easily saved by the fact that you don't have to write as many tests. Unless you aren't writing any tests, in which case, you have other problems.

      --

      Anthropic principle: We see the universe the way it is because if it were different we would not be here to see it.
    3. 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.
    4. Re:So many bugs by MightyMartian · · Score: 2

      Usually the compiler will scream "Hey fuckwad, your integer array ain't a string! Error on line 205", and then you dutifully go correct the error. Much better than a dynamic language that happily takes your screwup seriously and you have to follow a debugger chain through half a dozen nested functions to find where you buggered up.

      --
      The world's burning. Moped Jesus spotted on I50. Details at 11.
    5. Re:So many bugs by beelsebob · · Score: 2

      Here's an example:
      The null pointer dereference.

      With a properly set up type system, you express one type as the non-null pointer. You then express a second type - the maybe type, which describes something that is maybe something else. Now you use the Maybe Pointer as the result of allocation. Now in order to use the pointer, you need to get it out of the Maybe or the type system will complain, which enforces that you actually check for null before dereferenecing. It proves that your program will never dereference null.

      Obviously this is only one example, but yes, type systems really can eliminate entire classes of programming error. This is just one (particularly common) one that they can get rid of.

    6. Re:So many bugs by ebno-10db · · Score: 2

      when I work with dynamically typed languages I tend to spend similar amounts of times if not more figuring out what really is and is not supported with this dynamic object in front me

      Much worse than writing dynamically typed code is reading it. If I write function foo in a dynamically typed language, I know it's supposed to return an integer. If I'm reading somebody else's code, I often winding up guessing that an integer makes sense. Of course it may return a float or a string on odd Tuesdays when the moon is full. Much nicer for it to explicitly say it returns an integer, and have the compiler check that that's the truth.

  4. 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 Xunker · · Score: 2

      "I'm a hack hacker who hacks Hack."

      --
      Hilary Rosen's speech was about her love of money and her desire to roll around naked in a pile of money.
    3. 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?
    4. 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.

    5. Re:hate the name by globaljustin · · Score: 2

      this is my maxim: Geeks should not be allowed to name the things they create.

      "Hack" as a name for a programming language is egregious. It's like naming your newborn baby "Wipe"

      I wish my maxim weren't true, but it is.

      --
      Thank you Dave Raggett
    6. Re:hate the name by bitfarmer · · Score: 2

      > "Hack" as a language name? Really?

      Because "Brainfuck" was already taken.

      --
      Eagles may soar, but weasels don't get sucked into jet engines.
  5. 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.
    1. Re:Sarcastically Typed by Sarten-X · · Score: 2, Interesting

      I'm pretty sure that's Perl. You can define something first as a scalar, then refer to it as an array, because HA HA I WAS JUST KIDDING!

      You can also take a single variable and make it behave entirely differently depending on context. As a scalar, it works fine, but if you try to refer to it as an array, rm -rf /.

      Not saying you'd want to do those things, but you can.

      --
      You do not have a moral or legal right to do absolutely anything you want.
    2. Re:Sarcastically Typed by ysth · · Score: 2
  6. 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.
  7. 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.

  8. Re:static typing is awesome by Anonymous Coward · · Score: 2, Interesting

    that's great.. but the reason Scala is so sweet is that it does type inference. This means all your code is purposely typed even though you don't have to type all the tries (AC doesn't have to apologize for puns). This is going to be a nightmare where your last five dynamic functions his the bug that kills you.

    It's so sad that the fact that Mark got extremely lucky means that all this excellent computer science is wasted on a cesspool like PHP.

  9. Re:static typing is awesome by goombah99 · · Score: 2

    Many years ago I was a fierce opponent of static typing and loved the power of Obj-C and Python (was a NeXT/Mac head.) C++ and Java were crap (especially since Java didn't have type variables at the time.) Then I tried Haskell and my mind was duly blown. Now I'm a huge proponent of static typing, even if I still can't stand Java and avoid C++ unless necessary. IMHO Scala is the current sweet spot for statically typed general purpose programming language.

    I wish there was just some voluntary static typing in python. By this I don't mean run time voluntary type checking. that makes it slower. No I mean a pre-run time filter that optimizes the .pyc to the extent it can.

    --
    Some drink at the fountain of knowledge. Others just gargle.
  10. 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.
  11. Sounds like a good band-aid for PHP codebases by PhrostyMcByte · · Score: 3, Insightful

    Every few months someone announces a new fad language despite them rarely bringing anything new to the table, or the new things they do bring not being significant enough to warrant switching from some other well-established one.

    I'm actually happy with this one, because it serves an easier to justify purpose: migrating your existing PHP codebase and developers to something that is immediately better and familiar.

  12. 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.
  13. 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.

  14. 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.

  15. Re:English? by K.+S.+Kyosuke · · Score: 2

    convert the summary in english

    They reinvented soft typing. ;-)

    --
    Ezekiel 23:20
  16. Re:Dynamic typing is a hack by narcc · · Score: 2

    Ever tried to access 8-bit byte arrays and write them to a binary file in PHP?

    Yes, I have.

    It's hell and it takes a lot of work to go around all the stupid dynamic typing.

    No, it's not. It was simple and painless.

    Had you taken a few minutes to read the documentation, you wouldn't have suffered through "hell".

    Maybe programming isn't for you? Have you considered a career more suited to your talents?

  17. Re:English? by Wootery · · Score: 2

    Why go through all this effort when they could simply used one of the C-like strongly typed languages is beyond me.

    Because they already have a huge PHP codebase. That said, Twitter moved from Ruby to Scala for their back-end; it seems it can be done. I suspect Facebook have more of a lots-of-programmers problem: they all know PHP, and they might not all know, say, Scala.

    to creating a PHP-to-Java converter or something along those lines.

    You mean take PHP source and translate it into readable, maintainable Java source? That's all but impossible. They're very different languages, and source-to-source translators tend to produce pretty unreadable code when faced with that kind of task. A human wouldn't design a PHP program the same way she'd design the same program in Java. Without some terribly impressive AI, there's really no way around this.

    They could have translated PHP into totally-unreadable Java and then (run that through javac and) run the result on the JVM, sure, but then you're really just creating a rival PHP implementation. (Look like two separate projects have done just that, though, and apparently had pretty good results, outperforming the official PHP interpreter. All that says is that the official PHP interpreter is crap, which I think we all knew anyway.)

  18. 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.

  19. 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.
  20. Re:English? by turbidostato · · Score: 3, Insightful

    "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. "

    You mean, like any other interpreted languages you could choose?

    "The down-side has always been that the language also had many sloppy characteristics."

    You mean, like those avoided by any other interpreted languages you could choose?

  21. Re:English? by RabidReindeer · · Score: 4, Funny

    So not much different than PHP then...

    (ducks)

    It generates Perl?

  22. Re:Really... by narcc · · Score: 2

    So ... What would you have changed? What would your top-priority have been?

  23. 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).

  24. Re:English? by HiThere · · Score: 3, Interesting

    That's not what Object programming considers an object. It would be more like:
    "A struct and all the functions that use it as the first argument" depending on the language. Some languages would have it be a pointer to the above. One of the critical aspects of an object is that is has a local scope for each instance. In C this either means you are holding it on the stack, or you've done a malloc.

    That said, you can definitely do object programming in C. Existence proof is offered by valac -C which takes Vala code and emits a C equivalent. (It uses the GObject protocol to do so.) Mind you, the code that it emits is nearly unreadable, but it *is* object programming in C.

    --

    I think we've pushed this "anyone can grow up to be president" thing too far.
  25. 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});
  26. Re:English? by Pseudonym · · Score: 3, Insightful

    To be fair to Facebook, no other languages had those advantages in 2002-2003. Assuming you didn't want to use F/CGI, plugins for Apache or the equivalent, your server-side scripting options were JSP, Asp.NET and PHP.

    PHP was a reasonable choice under the circumstances, but they've now reached the limit of what it can do. It's good to see that they're moving to an entirely new and better language which just happens to have a similar syntax. If I was a Facebook engineer, I could live with that; syntax matters, but it doesn't matter as much as semantics and pragmatics.

    --
    sub f{($f)=@_;print"$f(q{$f});";}f(q{sub f{($f)=@_;print"$f(q{$f});";}f});
  27. Re:English? by Ash+Vince · · Score: 2

    Php (Personal Home Pages) is already something it was never intended to be. It's the "hey thats a nice feature, let me add a crappy implementation of it" language. If you mean "turn PHP into something never inteded to be" as turning it into something not absolutely horrible to work with then OK. Anything they do is going to be better than PHP. You would have to try really hard to make it worse.

    People always moan about how horrible PHP is, and I always assume that the people moaning are trying to learn the language without having a basis in C because they have come straight from Ruby or some other perfectly designed load of academic twaddle nobody uses.

    The reality is that PHP is like C, an amazingly flexible and well used tool. Yes, it has tons of quirks due to its slow evolution where they maintain backwards compatibility, but that is it's strength in the real world since nobody wants to rewrite their entire system just to use a new version of a language.

    There are tons of things better than PHP, but PHP is more well used than all of them because the purist developers who like to create perfect systems generally do not create much. The people who do are the armchair warriors who throw together something like Facebook then realise they have created a horrible puddle of code with a ton of technical debt that just happens to be a popular product that actually makes money. The purists have a habit of getting bogged down in producing wonderful code that takes ages, never makes any money and so get consigned to the scrap heap when the company folds.

    What they have done is layer a statically typed language on top of PHP so that they can still run their existing PHP code as they slowly convert it to a typed language. Maybe their end game is to move away from PHP, but maybe they will stay with Hack to they can carry on recruiting PHP developers who then have an easy transition. This is another reason why PHP is so popular with companies: commercially experienced PHP developers are much easier to find than commercially experienced Ruby developers, or even Python.

    --
    I dont read /. to RTFA, I read /. to offend people in ignorance.