3 ms·
No comment on decorators (I think they were a mistake to implement at all, and the TS team agrees), but extension functions? Putting aside the question of how
by comp_throw7 5y ago
No comment on decorators (I think they were a mistake to implement at all, and the TS team agrees), but extension functions? Putting aside the question of how often it makes sense to uses classes at all in Typescript, you can always just... extend a class (or interface) with whatever function you want? It's not like Typescript has sealed classes/interfaces. I might be misunderstanding?
Most everything else seems to come down to "Typescript is meant to be runtime-only", which definitely has upsides and downsides (I agree that the lack of meaningful reflection support is annoying).
- ptx 5y agoLet's say we have a list of strings and want to filter for those starting with "a". In Kotlin, we can use an extension function defined on the standard Java ArrayList object: myStringList.filter { it.startsWith("a") } ...and on arrays: myStringArray.filter { it.startsWith("a") } ...and on, for example, a boolean array (a standard JVM primitive array object): myBooleanArray.filter { it != false } This works for any kind of array and any kind of list, etc., even though the objects are ordinary JVM arrays and lists. In Java, because it lacks extension functions, the stream functions are often not accessible from the object itself, so you have to hunt for where they've decided to put them for this particular type. In the first case, we do have a stream method on the list: myStringList.stream().filter(s -> s.startsWith("a")).toList(); But in the case of arrays, we have to hunt down a static method in the Arrays class: Arrays.stream(myStringArray).filter(s -> s.startsWith("a")).toArray(); And in the last case, with a boolean array, that method doesn't work and the solution I've seen suggested is this wonderful mess (involving a different static method on a different class): IntStream.range(0, myBooleanArray.length).mapToObj(i -> myBooleanArray[i]).filter(b -> b != false).toArray();
- lf-non 5y ago> you can always just... extend a class (or interface) with whatever function you want Not quite. Extension functions are statically resolved and abide by usual access modifiers. So within a class, I can have a private extension method for another class (say C). Now if my class receives any instance of C, I can call these extension methods on this instance. But when I forward this instance to someone else, they wont have access to any of these extensions. Libraries like exposed use this quite extensively. > Most everything else seems to come down to "Typescript is meant to be runtime-only" Yes, this one too because it would involve type directed emit.