Please create an account to participate in the Slashdot moderation system

 



Forgot your password?
typodupeerror
×
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:
  • Obviously a backdoor for the NSA 8)
  • So many bugs (Score:5, Insightful)

    by ClayDowling ( 629804 ) on Thursday March 20, 2014 @03:07PM (#46537699) Homepage

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

    • Re: (Score:3, Insightful)

      by Anonymous Coward
      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 twi
      • Re:So many bugs (Score:5, Insightful)

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

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

        And then you get to debug...

      • I get sick of spending half my time dicking around with static types, casts, etc and spinning my wheels chasing down type errors

        If you spend half, or any appreciable part of your programming time dealing with static typing issues, then you're doing something seriously wrong. Occasionally the compiler will be a pain in the ass about it, but most of the time static typing should require nary a thought. If it's otherwise, you're not thinking about types clearly. The static typing is there to catch you when you screw up. It also serves as useful documentation for the next poor schmuck that has to look at your code, and for good measure

      • Dynamic typing frees your mind to think about the problem at hand and the best solution to that problem

        Quite the contrary. It frees your mind to think about the problem incorrectly, then requires your mind to churn through many incorrect ideas before finally settling on the correct one. Meanwhile, static typing deals with a large chunk of correctness for you, and gets you to the correct solution faster by pointing out the errors before you hit them at runtime.

        I get sick of spending half my time dicking around with static types, casts, etc

        If you're casting, you're not thinking about the types correctly yet. Learn to code a bit better.

        I'll take dynamic typing any day and be done twice as fast,

        No, you won't. Because every time the static type

        • Otherwise, you'd have to run your program, and discover that type error at runtime.

          Don't be absurd. Type problems don't show up during testing - they show up 3 weeks after the code has been released, and then only in situations that are practically impossible to reproduce. That makes debugging more interesting. Any idiot can find type problems when the compiler does it for you.

    • 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.
  • hate the name (Score:5, Insightful)

    by mr_mischief ( 456295 ) on Thursday March 20, 2014 @03:07PM (#46537707) Journal

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

    • by swb ( 14022 ) on Thursday March 20, 2014 @03:09PM (#46537717)

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

    • by Xunker ( 6905 )

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

    • by pushing-robot ( 1037830 ) on Thursday March 20, 2014 @03:15PM (#46537771)

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

    • It shows they have a sense of humor, given the effort it must have taken to make a 'better PHP'.

    • I agree, I'm just replying to undo the incorrect moderation I just did. Fucking phone UIs.
    • Re:hate the name (Score:5, Insightful)

      by WillgasM ( 1646719 ) on Thursday March 20, 2014 @03:32PM (#46537947) Homepage
      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.
      • 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.

        Uh, people exploiting Facebook?

        Ironically, you have that completely backwards.

        Don't feel bad for making that common mistake. The billion other users did too.

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

    • > "Hack" as a language name? Really?

      Because "Brainfuck" was already taken.

    • Okay, stupid name aside, this is awesome. I've never had a single good thing to say about Facebook or Mr. Zuckerberg, but this could totally change that. Lots of devs disparage PHP, but they're all idiots -- PHP is heavily used because it's heavily useful. I haven't used HACK yet, but if it's not a buggy piece of junk might truly be great. I've yet to find a language that lets me go dynamic when I'm prototyping but gradually type when I see fit. So...Sweet!

      That said, static typing isn't all it's cracked up

      • > Lots of devs disparage PHP, but they're all idiots

        or they haven't looked closely at the newest changes to the language in the last two years.
        Or they are talking more about the "PHP community", thousands of "scripters" who use PHP because it's easy to build things in PHP, in the same way that it's easy to build things with Legos.

        I helped write the PHP certification test, which suggests I know a little something about PHP. My PHP code is used by many large universities. I could go on, but suffice to s

  • by wisnoskij ( 1206448 ) on Thursday March 20, 2014 @03:08PM (#46537715) Homepage

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

  • by afidel ( 530433 ) on Thursday March 20, 2014 @03:10PM (#46537731)

    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.

  • Nope (Score:5, Insightful)

    by Sarten-X ( 1102295 ) on Thursday March 20, 2014 @03:15PM (#46537769) Homepage

    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.

    • by narcc ( 412956 )

      PHP lacked vital modern features (like static typing

      Modern features like static typing.

      Modern features like static typing...

      Seriously?

    • The whole point of this project is back-compat with PHP. If you don't need that, then there are already half a dozen better languages which have all those other new features...

  • by PhrostyMcByte ( 589271 ) <phrosty@gmail.com> on Thursday March 20, 2014 @03:21PM (#46537835) Homepage

    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.

  • I guess it wouldn't hurt to have static typing in PHP, but for the love of god, why not just pick a more standard syntax?

    Some of their examples:

    ?hh
    function f1(): ?resource { ...
    }

    public function bar(): (string, string) {

    Might look good in some languages, but in PHP I would really expect that type before the function name.

  • I'd have thought the horrible global function namespace would have been a top priority.

    Seriously [twimg.com], what is this, C?

  • Microsoft added static typing to Javascript (with Typescript). Now Facebook is adding static typing to PHP. After a few years of dynamic languages being in vogue, is the pendulum swinging back to statically typed languages?
     

    • Yes, mainly because there are literal mountains of shit code that no one can debug, and all of a sudden the notion that maybe having basically one variable type is a bad thing.

    • by g8oz ( 144003 )

      Difference is the static typing here is "opt-in". I think that helps. "If you like your current type practices you can keep them".

  • I've contemplated a language/library with extra strict typing for doing real-world calculations.

    For example, if floating point variables x and x were be classed as 'Lengths', and you stated "a = x * y;" a would have to be classed as an Area.

    If you then stated "a *= 2;" a would still be an area (of twice the size), but "v = a * x;" would return a Volume class, and "a = a * x;" would be a compile time error (trying to assign a Volume result to an Area class)

    other included types would be Time intervals, with a

    • Some languages do have what you want, e.g. F# [microsoft.com]. Others allow this to be done as a library, like C++ [boost.org].

      But this feature is sufficiently niche that I wouldn't expect it in PHP.

    • Not a bad idea. If your classes have no virtual methods and they don't allow mixins or other crazy runtime dynamic shenanigans, then in theory the compiler can optimize them down to un-boxed numbers. I wonder how man OO languages are very good at that though. The hard part might be the base class. If you don't even have un-boxed numbers and the base class has virtual methods... you're kind of stuck with something less than optimal.

      Oh and of course time*length isn't velocity. length/time is. Division i

    • I've contemplated a language/library with extra strict typing for doing real-world calculations. For example, if floating point variables x and x were be classed as 'Lengths', and you stated "a = x * y;" a would have to be classed as an Area.

      You want C++11. You can infer the Kg/m/s powers etc using a small amount of template hackery. The thing that C++11 adds is user defined literals, so you could type:

      length a = 4m;

      which gives it a syntax whih doesn't suck.

  • One of the first things you should consider when choosing a name for your project these days is: how relevant will search results be when people Google for it?

  • Sorry, slop and rigor don't really combine well.

Don't get suckered in by the comments -- they can be terribly misleading. Debug only code. -- Dave Storer

Working...