by comp_throw7 · 4y ago · 0 comments
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).
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();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.