r/programming • • Mar 20 '14

Facebook introduces Hack: a new programming language for HHVM

https://code.facebook.com/posts/264544830379293/hack-a-new-programming-language-for-hhvm/
802 Upvotes

526 comments sorted by

View all comments

-2

u/[deleted] Mar 20 '14

Awesome. These are some things I wish the official PHP would adopt. I just wish FB wasn't developing this.

Some criticisms:

  • Why are types specified with the same syntax other languages use for extension?

Their example

public function increment(int $x): int
{
    $y = $x + 1;
    return $y;
}

I'd rather see

public int increment(int $x)
{
    $y = $x + 1;
    return $y;
}

which also happens to fall inline with their "less verbose" goal. Also, can you use arbitrary types here? If I have a class MyType, can I do this?

public function foo() : MyType
{
    return new MyType();
}
  • Why in god's name are nullable types specified with ?bool instead of bool?? Do any other languages put the question mark first? Was the concern that it would be confused with Ruby's something.valid?? There's no examples of this [Ruby's way] in PHP or HACK that I'm aware of.

  • The lambda syntax. Come on. FB, you invented a totally unfamiliar syntax for this, and it's totally verbose. I have to specify a method name, and then the method signature, and then the return type for the method signature, and then what the heck is ==>? The "identity greater than" operator? Is this yielding a value!? What is it!?

Make the syntax sane:

var $xPlusOne = int $x => $x + 1;

or, to rewrite the example provided:

void test()
{
    $y = 'baz';

    echo string $x => {
        $x . $y;
    }
}

http://msdn.microsoft.com/en-us/library/bb311046.aspx

http://www.secnetix.de/olli/Python/lambda_functions.hawk

10

u/dparoski Mar 20 '14

Engineer working on Hack here.

Syntax is one of those things that people can debate about endlessly and where individual's opinions tend to change over time (i.e. the programmer's first impression is often different than how the programmer feels about it after having used it for a little while).

Hack's syntax was generally driven by few competing concerns, including: brevity, familiarity for programmers who are already familiar with PHP and other languages with similar syntax, smooth interoperability with PHP, the ability to easy search a code base with regex-based tools ("grep-ability"), and avoiding genuine ambiguities between different language constructs.

For example, for lambda expressions we opted to go with "==>" because we didn't want to conflict with PHP's "=>" symbol which is currently used with array literals, foreach, and yield expressions (and might possibly be used for other things in PHP 5.7 and beyond). We figured "==>" would be recognizable enough that people would know what was going on. Yes, it would technically be possible to co-opt the "=>" symbol for lambda expressions, but then you'd have genuine ambiguities that would need to be resolved by arbitrary disambiguation rules (for example, you'd have to decide whether the "=>" in "array($x => $x+1)" was a lambda expression) and we didn't want programmers to get frustrated or confused in such cases, plus we wanted to make sure lambda expressions were easily recognizable to readers of the code.

2

u/Uncompetative Mar 21 '14

I loved reading the explanation behind the choice of the ==> here, I found it very amusing, usually CS is so dry:

http://docs.hhvm.com/manual/en/hack.lambda.operator.php

5

u/Nomikos Mar 20 '14

The lambda syntax. Come on. FB, you invented a totally unfamiliar syntax for this, and it's totally verbose.

The first example seems silly, but if you don't need a function that returns a function they're rather compact: http://hacklang.org/manual/en/hack.lambda.examples.php

and then what the heck is ==>?

Aliens!

3

u/[deleted] Mar 20 '14

Ah, okay. So their lambda syntax is sane, but it's the closure syntax I'm complaining about. Thanks for clarifying what ==> is.

Now I wish they co-opted ->. ==> looks too much like a comparison operator to me (not that => doesn't).

0

u/[deleted] Mar 21 '14

It looks like a god damn arrow, jesus christ why are you typing so much when all you really mean to say is "I'm a shit programmer who has never seen lambdas before) holy fuck.

2

u/[deleted] Mar 21 '14

What?

1

u/hearingaid_bot Mar 21 '14

IT LOOKS LIKE A GOD DAMN ARROW, JESUS CHRIST WHY ARE YOU TYPING SO MUCH WHEN ALL YOU REALLY MEAN TO SAY IS "I'M A SHIT PROGRAMMER WHO HAS NEVER SEEN LAMBDAS BEFORE) HOLY FUCK.

1

u/[deleted] Mar 21 '14

Oh okay, thanks hearingaid_bot. I get it now.

5

u/pipocaQuemada Mar 20 '14

Why are types specified with the same syntax other languages use for extension?

What languages other than C# use ':' for inheritance? Also, ':' or '::' has been used in dozens of languages for type ascription dating back to the 70's. It's not a new syntactic invention by any means. Check out e.g. SML, OCaml, Haskell, Scala, F#, Clean, Miranda, etc.

The lambda syntax. Come on. FB, you invented a totally unfamiliar syntax for this, and it's totally verbose. I have to specify a method name, and then the method signature, and then the return type for the method signature, and then what the heck is ==>? The "identity greater than" operator? Is this yielding a value!? What is it!?

I assume you mean the snippet

function foo(): (function(string): string) {
  $x = 'bar';
  return $y ==> $x . $y;
}

I don't really know Hack, but it looks like it's the equivalent of the C#

Function<String, String> Foo() {
  String x = "bar";
  return (y => x + y); 
}

That is to say, you neither need to specif a method name nor a method signature, and ==> is the actual syntax for the lambda. It's not really that strange, especially because a number of languages use => for lambdas. Since it's a first class value, you can return it from a method, and if you want to give it a type, the type is "function(string): string".

1

u/[deleted] Mar 20 '14

I wondered if the type following the signature was a functional programming thing. Seeing as FB works with OCaml, I shouldn't be surprised.

Yes, I was (unintentionally) referring to the entire closure. I thought that was their solution for lambdas. /u/Nomikos pointed this out.

1

u/bstamour Mar 21 '14

C++ also uses : for inheritance. Probably where C# got it from.

4

u/TJ09 Mar 20 '14

Personally, I've found that return types get lost inside function modifiers.

Especially when you have something like "public static async function"

4

u/gclaudiu Mar 20 '14

I don't work on the Hack team, but I write Hack every day at work. Being able to grep for functions easily without knowing the return type is pretty cool. I also find it easier to read, as you point out.

1

u/[deleted] Mar 20 '14

You can do the with, without, and regardless of the return type.

grep -re "public static async .\+ Method" .

1

u/gclaudiu Mar 20 '14

I'm not saying it can't be done, I'm saying it's easier.

The thing about syntax is that you'll embrace what you're used to. Give it a shot, you'll get used to and start seeing it with different eyes.

1

u/[deleted] Mar 20 '14

But that would be

public static async void Foo() {}

It hasn't been an issue for me because the type is always preceding the name. C# even enforces this - it's invalid not to order the type and name this way.

With IDEs, it's even less of an issue because they tell you what types you're dealing with. The same would be true with the type at the end.

Nixing IDEs, to me, I'd be lost with the type at the end, especially because developers have so many different styles regarding that part of code styling.

public static async function() : type
{
    ...
}

public static async function() : type { ... }

public static async function() : type {
    ...
}

public static async function($foo, $bar, $baz) : type {}

public static async function(
    $foo,
    $bar,
    $baz) : type {}

public static async function(
    $foo,
    $bar,
    $baz)
: type {}

    public static async function(
    $foo 
    ,$bar = null
    ,$baz = "qux")
: type {}

    public static async function(
    $foo 
    ,$bar = null
    ,$baz = "qux") : type {}

1

u/[deleted] Mar 20 '14
public static explicit operator int

3

u/Daniel15 Mar 20 '14

Also, can you use arbitrary types here? If I have a class MyType, can I do this?

public function foo() : MyType { return new MyType(); }

Yeah, that should work. There's also a "this" return type which specifies that the method returns the class itself (usually for method chaining / fluent APIs)

class Foo {
  public function setBar(string $value): this {
    $this->value = $value;
    return $this;
  }
}

0

u/[deleted] Mar 21 '14

[deleted]

1

u/[deleted] Mar 21 '14

I'd rather type this:

int Increment(int x) {
    return x+1;
}