It’d be cool if you could annotate internal-only types to minify them (at the expense of not being directly usable from JS). Basically an automated version of @as:-
@minified
type example = {
someDescriptiveProperty: int,
otherDescriptiveProperty: int,
}
let empty = {
someDescriptiveProperty: 0,
otherDescriptiveProperty: 0,
}
It is for size optimisation. I think the only minifier that can do this is Google Closure Compiler in advanced mode can do this optimisation.
But I’m pretty sure you’d be able to do better too - since GCC has a global mapping for all object keys, but you’d do a per-object mapping, so could end up with shorter names.
Right; sorry I momentarily forgot that most minifiers aren’t that good lol.
Fwiw, historically these compiled to arrays, so were even better in terms of size theoretically. But after we’ve measured we realized it basically made no difference. That and the debuggability and zero learning/runtime overhead of interop (and some perf gains from no bound checking) made us go through with changing them to js objects.
It’s still good to care about output size though.
The @as feature was made for interop reason back when our syntax didn’t have the \"illegal-identifier" feature. You can of course use it to minify but at that point I’d suggest focusing elsewhere, e.g. remove a big dependency, which has a few orders of magnitude bigger ROI.
I sometimes ponder how this could be achieved, perhaps record annotations as compiler hints? Default to renaming, but with a FFI hint to disable rename? I would also accept only renaming with a hint (but then I’d be littering my codebase with hints).
This hint would also be a useful way to add a "don’t emit optional fields with value None" capability, this is essential for some APIs. The need to remove optional keys is the only reason I still use @deriving(abstract) and @obj.