Please create an account to participate in the Slashdot moderation system


Forgot your password?
PHP Facebook Programming

Facebook Introduces Hack: Statically Typed PHP 230

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.
This discussion has been archived. No new comments can be posted.

Facebook Introduces Hack: Statically Typed PHP

Comments Filter:
  • by Anonymous Coward on Thursday March 20, 2014 @04:13PM (#46537757)

    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.

  • by Sarten-X ( 1102295 ) on Thursday March 20, 2014 @04:18PM (#46537811) Homepage

    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.

  • Re:English? (Score:5, Interesting)

    by alokmenghrajani ( 2602135 ) on Thursday March 20, 2014 @04:30PM (#46537933) Homepage
    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.
  • Re:English? (Score:5, Interesting)

    by MightyMartian ( 840721 ) on Thursday March 20, 2014 @05:27PM (#46538385) Journal

    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.

  • Re:English? (Score:4, Interesting)

    by HiThere ( 15173 ) <> on Thursday March 20, 2014 @11:02PM (#46540427)

    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.

"Wish not to seem, but to be, the best." -- Aeschylus