On Thu, Sep 17, 2020, at 10:21 AM, Matthew Brown wrote: > Quick thing before I get into my own reaction: > > Transpiling is normally thought of as the process of converting one > language into another. Tools like Babel transpile TypeScript to JavaScript. > > What's being proposed here (AFAICT) is type erasure – the generic type > information would be erased during the conversion to opcodes. > > Python (the language) has type erasure for all its types (including its > generic types), so no popular Python interpreters check that a function > call's argument types match up with the function signature. > > PHP currently does not erase any types so the opcodes generated by PHP's > interpreter include type checks for all the arguments passed to a typed > function signature. > > Hack follows PHP's model, but erases generic types by default (though has > more recently introduced the concept of reified generics) in much the same > way you're proposing.
I concur; this is type elidiing, not transpiling. Transpiling implies a user-triggered pre-run step. I know Sara has mused about it before but it's not happened yet. Basically what is being proposed (for those who need a concrete example) is you'd write this: class Collection<T> { public function add(T $item) { ... } } $c = new Collection<Product>(). And linters would recognize that, and it would parse, but at runtime it would compile to: class Collection { public function add(mixed $item) { ... } } $c = new Collection(). And that's the opcodes that would be saved. (I'm hand-waving a lot here.) I would be on board with this, although I would ask if it's possible to provide some compile time checks; I'm thinking LSP validation, which is still relevant for inheritance with generics. Beyond that, leave the runtime to not enforce it. (I have no idea how viable that is, but Sara seemed to think it would work so it probably would.) Another possibility could be to desugar that original to: class Collection { public $__T; public function add(mixed $item) { if (!$item instanceof $this->__T) { throw new TypeError(); } } } $c = new Collection(). $c->__T == 'Product'; Which is essentially what you can do in userspace today, but automated. I don't know how viable that is, but it's another thing to consider. --Larry Garfield -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php