98 points · by moebrowne · 1y ago · 63 comments
More generally, it's weird to see a whole blog post about generics for PHP not even mentioning Hack's generics designs. A lot of thought and iteration went into this like 5-10 years ago.
See https://docs.hhvm.com/hack/arrays-and-collections/object-col... and https://docs.hhvm.com/hack/arrays-and-collections/vec-keyset...
But after some quick checking, I learned that Hack is still actively maintained, surprisingly.
What a weird take to base your current belief on something that happened more than a decade ago.
Not only the condition for Hack creation (speed, memory usage and strict type checking) have been fixed a long time ago since php 7.0
But also if you reach 10% of facebook scale, it doesn't matter what language you used, you will need to rewrite anyway.
Show me a company where PHP is the issue because they reached 10% of facebook scale, and what you're showing is a company that succeeded thanks to PHP. Applies to other language the same. Picking your stack based on "but what if I reach that scale" has to be the mother of all premature optimisations.
The answer to "why Hack" needs to be viewed in the historical context of "when Hack" and what was happening (or not) in the php ecosystem at that time.
Things have changed a lot since, in terms of performance, language longevity, ecosystem etc. Its a perfectly reasonable language to adopt for many orgs.
At Facebook scale — with thousands of engineers shipping new code twice a day — slowdowns like these are even more problematic.
https://engineering.fb.com/2014/03/20/developer-tools/hack-a-new-programming-language-for-hhvm/I wish we had typed arrays. Totally not gonna happen, theres been RFCs but I have enough boilerplate classes that are like
Class Option Class Options implements Iterator, countable, etc.
Options[0], Options[1], Options[2]
or Options->getOption('some.option.something');
A lot of wrapper stuff like that is semi tedious, the implementation can vary wildly.
Also because a lot of times in php you start with a generic array and decide you need structure around it so you implement a class, then you need an array of class,
Not to mention a bunch of WSDLs that autogenerate ArrayOfString classes...
This is the core problem with PHP for me.I love PHP and use it every day. Part of that is the strength and versatility of the arrays implementation (i.e. hashmap). However, the problem is always the fact that an array cant be typed.
IF they could just introduce that, it would solve 80% of user-land issues over night.
Is this not suitable for you?
C# is using reified generics where this information is preserved. List<String> is still List<String> after compilation
This is very important both for cache locality and for minimizing garbage collector pressure.
> This is very important both for cache locality and for minimizing garbage collector pressure.
How is C# just not straight-up faster than Java then? Instead of both language punching around the same weight on benchmarks? Doesn't cache locality like, have a huge effect on performance?
public class StringList extends java.util.ArrayList<String> {
public static void main(String[] args) throws Exception {
StringList asStringList = new StringList();
java.util.ArrayList<Integer> asArrayList = (java.util.ArrayList<Integer>)(Object)asStringList;
System.out.println("It knows it's an ArrayList<String>: " + java.util.Arrays.toString(((java.lang.reflect.ParameterizedType)asArrayList.getClass().getGenericSuperclass()).getActualTypeArguments()));
System.out.println("But you can save and store Integers in it:");
asArrayList.add(42);
System.out.println(asArrayList.get(0));
System.out.println(asArrayList.get(0).getClass());
System.out.println("Unless it's static type is StringArrayList:");
System.out.println(asStringList.get(0));
}
}
That prints out: It knows it's an ArrayList<String>: [class java.lang.String]
But you can save and store Integers in it:
42
class java.lang.Integer
Unless it's static type is StringArrayList:
Exception in thread "main" java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String (java.lang.Integer and java.lang.String are in module java.base of loader 'bootstrap')
at StringList.main(StringList.java:11) class Foo<X> {
X x;
}
Foo<int> fooInt;
fooInt.x = 5;
Foo<float> fooFloat;
fooFloat.x = 5.0;
compiles to: class Foo_int {
int x;
}
class Foo_float {
float x;
}
Foo_int fooInt;
fooInt.x = 5;
Foo_float fooFloat;
fooFloat.x = 5.0;
On the other hand, erased generics compiles to this: class Foo {
void* x;
}
Foo fooInt;
fooInt.x = new int(5);
Foo fooFloat;
fooFloat.x = new float(5.0);I believe the terms reified generics and erased generics is the type sweaty donkey ball terminology you get for professional CS academics.
Sticking my neck out further.
Reified generics means the type is available at run time. In C# you can write if(obj.GetType() == typeof(typename))
Erased generics the type information is not available at run time. That's the way Java does it and it kinda sucks.
Academic jargon isn't invented to be elitist, it's invented to improve communication.
(of course there's a good chance you understand this already, and you're just making a dumb joke, but I figured I'd explain this anyway for the benefit of everyone reading)
Regardless, I recognize myself as the point of failure, but those names do strike me as academia speak, though better than some/many. <shrug>
In a good statically-typed language you don't need runtime type information. It could be a Void in the bytecode for all I care, as long as it behaves correctly.
> obj.GetType() == typeof(typename)
In a statically-typed language, this can be optimised away to a bool at compile time.
To be more precise: in Java, generics on class/method/field declarations are available at runtime via reflection. The issue is that they aren’t available for instances. So a java.util.ArrayList<java.lang.String> instance is indistinguishable at runtime from a java.util.ArrayList<java.lang.Object> instance
class BlogPostRepository extends BaseRepository<BlogPost> { ... }
$repo = new BlogPostRepository();
but the following would be very hard: $repo = new Repository<BlogPost>();
They write that the latter would need runtime support, instead of only compile time support. But why couldn't the latter be (compile time) syntactic sugar for the former, so to speak?(As long as you don't allow the generic parameter to be dynamic / unknown at compile time, of course.)
As a community, we've seen enough untyped PHP spaghetti code in the early 2000s and never want to go back there.
That's a weird way to think about generics. Any class using generics effectively functions as a version of the class where the types are explicitly written out in all places. Instead of writing out one version of the class for every generic type, you write it out once and say which version you want to use at instantiation. You lose no safety, it's purely cosmetic and quality of life.
One of the most sought-after features for PHP is Generics: The ability to have a type that takes another type as a parameter. It's a feature found in most compiled languages by now, but implementing generics in an interpreted language like PHP, where all the type checking would have to be done at runtime, has always proven Really Really Hard(tm), Really Really Slow(tm), or both.
* The topic of the article is implementing generics at compile time, but this claims that PHP is not compiled.
* Type checking is orthogonal to compilation vs interpreter.
* Types are not checked at runtime. It is kinda the point of types that they are checked before code runs. Runtime checks are on values. You can reify types at runtime but this breaks a useful property of generics (parametricity) and it prevents the very useful feature of types without a runtime representation (often known as newtypes).
* If you want to use types in the colloquial "dynamic type" meaning as tags on values, and you also want to talk about generics (a feature that only makes sense for types-as-compile-time-properties) you need to be really careful in your terminology or confusion will abound!
I wish they would elaborate on what Really Really Hard(tm) means. From a high-level I believe everything they are concerned with has been researched as part of the work on contract by Robby Findler and collaborators. E.g. https://users.cs.northwestern.edu/~robby/pubs/papers/popl201...
But really while I was reading up on what generics are I went, isn't that just python, strongly typed but your functions don't have built in type checks.
For example, a mergesort algorithm works on any kind of array, as long as you can compare the elements in the array to each other. There's no point in re-implementing the algorithm for each kind of array. Yet, without generics, you'd need to do just that. At the same time, the generated code for sorting each different kind of array might need to be a little different - comparing strings and floats isn't the same assembly, for instance. So the programming language and compiler work together: you specify the algorithm once in a certain way, and the compiler can generate the right version of algorithm for each way that you need to use it.
There are many, many good reasons why you might want to work in a typed language, even though specifying the types is a bit of extra book-keeping; generics are one way to keep the pointless work down. Of course, if you can get away with a python script, there's no need to bother with all this typing business just yet, either.
It's rather the exact opposite. Parametric types are a way to properly type "deeply" instead of just the topmost layer. Just like inference, type parameters don't remove types.
> isn't that just python, strongly typed but your functions don't have built in type checks.
That doesn't really make any sense? Static types mean you don't have runtime type checks, since the types are known statically.