by orangeeater · 7y ago · 0 comments
Can you explain what Java offers with regard to reflection that JS doesn't?
My feeling is that reflection is almost moot in JS since you can inspect/mutate objects at runtime however you like. But maybe I'm missing the point of reflection.
There is a Reflect object with a bunch of static methods on it in ES6. It mostly just replicates functonality that already exists in the language, and my suspicion is that it's mainly there so it can be extended at a later date without breaking backwards compatiblity.
Among hundreds of other things, getting the return type of a method and getting the input parameter names (+ types) in a way that doesn't revolve around literally parsing the functions toString() representation.
Oh, and typeof checking that isn't disgustingly broken.
Type information literally doesn't exist at runtime, though. Isn't it kind of ridiculous to compare languages like that?
Correct if I'm wrong, not all types get erased in Java. Doesn't type erasure only happen for generics? Say I have a non generic, plain Java class and want to inspect one of it's method's return types at runtime to see if it returns class A or class B. I can do that, right?
Ah, that's quite cool.
Which in some ways is a good thing as you can do a lot more optimizations.
Maybe I'm just complaining that JS is too dynamic, but being able to generate code at compile time with reflection and know it will generally work (type checked) is nice.
Tip: in JS, I don't want OOP class. If you really want class, could you show me the intention behind it?
Adding another example, JPA MetaModel generator that allows you to make strongly typed calls in ORM.
Basically, it let's you generate code at compile time that's seamlessly integrated into the existing code. So it's generated code that still gives you IDE autocomplete support etc.
I automagically map some postgres JSONB columns to a parsed object form. Looks just like a regular typed nested object.
You basically HAVE to do this in Java otherwise your program won't work. In JS, you can look everything over at runtime and dispatch from there (made much easier because first-class functions and closures are a thing).
router.get('/users/:id', (id: UUID) => ...)
router.get('/products/:id', (id: Int) => ...) router.get('/users/:id'. skipNonUuid(id => ...))
You simply have a function/middleware that does the check.... or `compose(skipNonUuid, dbo.findUserById, ui.formatView('modelName'))`You can do function composition that is nearly as simple and far more flexible in general with JS
There's no way to hook into the loading of modules in a standard way - you have to know how the modules are compiled and required.
One thing comes to mind, how to twinkle stuff inside closures?