hhvm icon indicating copy to clipboard operation
hhvm copied to clipboard

Allowing Collections to be instantiated by `new Collection`

Open morrisonlevi opened this issue 12 years ago • 4 comments

During Hack Developer Day I attempted to port some of my code to use Hack. During that I found this interesting little bit:

$v = new Vector();

This fails the type-checker; you have to pass null. Here's the log of when I asked about it on the Hack Developer Day 2014 page on Facebook: https://www.facebook.com/morrison.levi/posts/1438138886425227

Basically I was told to use $v = Vector {}.

Just a moment ago in IRC I mentioned it again and was told by @jwatzman that using new Collection is deprecated.

In my opinion this is a bad decision. User defined types and language core types should look and behave similarly; this is a basic principle of language design.

tld;dr I should be able to create a new Vector using idiomatic PHP code: new Vector().

morrisonlevi avatar Apr 25 '14 16:04 morrisonlevi

Copying the relevant bits from the Facebook thread into here:

@elgenie The code "new Vector($iterable_to_initialize_from = null)" looks like a bad code smell because it is. It moves you into using code like:

$a = new Vector(null); $a[] = 'x'; $a[] = 'y'; $a[] = 'z';

... as opposed to:

$a = Vector {'x', 'y', 'z'};

The latter opens up some optimizations of literal expressions in addition to being shorter to read, consistent (aka possible) with the Imm{Vector,Set,Map} collections, etc.

.

@morrisonlevi I'm making a Vector<Vector<Point>> where each dimension is 1024; I'm not going to write that inline ^^

.

@elgenie Well, you still can:

$a = Vector {}; $a[] = 'x'; $a[] = 'y'; $a[] = 'z';

And your point:

In my opinion this is a bad decision. User defined types and language core types should look and behave similarly; this is a basic principle of language design.

I think this is an interesting point -- though I'm pretty sure all collections are final so you can't sublcass them anyways. I think it would be useful to get a response from @paroski (who wrote most of collections) and/or @elgenie on the reasoning here so, at the very least, we can add it to our docs.

One advantage of the literal syntax, that doesn't invalidate the consistency argument, is that you can write stuff like this that isn't legal with new:

<?hh
function f(Vector<int> $x = Vector { 1 }) {
 // ...
}

jwatzman avatar Apr 25 '14 16:04 jwatzman

To be clear, I'm okay with literals syntax; I simply think not allowing it to also be instantiated in the normal, idiomatic way is a mistake.

morrisonlevi avatar Apr 25 '14 16:04 morrisonlevi

Constructing objects with new and no arguments is normal and idiomatic if and only if the given class has decided to support that as the signature for its constructor; in the abstract, "idiomatic" is a way of stating that "this feels right in my subjective opinion". new Collection is not deprecated, it just targets a different initialization usecase than the one that you seem to want.

Perhaps that's the decision that you're not pleased with. @paroski decided that, given the presence of literal syntax, we should move the "initialize from another container-like thing" as being the initialization pattern made available in the constructor, as opposed to other possibly reasonable choices like taking a variadic list of the items to put into the container. He can speak better for the reasons for that choice; I think it should be possible for HHVM to realize some perf wins via using explicit literals to produce better bytecode, and taking a variadic of elements in the constructor creates bad incentives to use arg unpacking ... instead of the faster initialize-from-iterable.

However, with the choice for what the constructor is having been made, defaulting iterable to support zero-argument initialization (new Vector()) only muddies the waters in terms of how collections are to be constructed. new Vector(function_that_returns_iterable_or_perhaps_null()) is the current intended use case for the constructor.

Collections appear in the static initializer grammar subset and no programmer-created object ever can, and the very presence of the static initializer grammar ensures that programmer-defined types cannot ever behave similarly to collections. arrays have not one but two special initialization syntaxes, neither of which is new even though both of the initialization syntaxes unambiguously allocate a new container.

User defined types and language core types should look and behave similarly; this is a basic principle of language design

"Look and behave similarly" is not a free lunch. There are pretty clear tradeoffs here with losing the ability to make truly distinct core type behavior appear as distinct as it is.

In general, I think having two syntaxes to accomplish the exact same thing is generally a smell of bad design and not fully thought out APIs. Since collections literals must exist for static initialization, property initialization, and argument defaulting, I don't think it's especially crazy to make them the blessed way of declaring a new collection.

elgenie avatar Apr 29 '14 12:04 elgenie

Sounds like this isn't likely to change; closing.

jwatzman avatar May 09 '14 22:05 jwatzman