3 ms·
> getting the return type of a method and getting the input parameter names (+ types) Type information literally doesn't exist at runtime, though. Isn't it kin
by pitaj 8y ago
> getting the return type of a method and getting the input parameter names (+ types)
Type information literally doesn't exist at runtime, though. Isn't it kind of ridiculous to compare languages like that?
- orf 8y agoWhen you're comparing the relative power of reflection within the language, absolutely not.
- untog 8y agoBut what would you use that information for, given that the language has no types? A JS function has no guarantees of what type a function returns anyway. It's like taking issue with the fact that a car doesn't come equipped with skis.
- stickfigure 8y agoIt's like taking issue with the fact that a car doesn't come equipped with skis. To draw out that analogy... we live in Greenland. Cars with skis are surprisingly useful.
- lopatin 8y ago> Type information literally doesn't exist at runtime, though. 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?
- stickfigure 8y agoYes, you are correct. There are also a number of circumstances in which you can get generic type info - it isn't erased, for example, if you create a concrete subclass of a generic type.
- pitaj 8y agoI was talking about JavaScript. In JavaScript there are no typed functions or arguments, so that information doesn't exist period.
- lopatin 8y agoRight, so I guess I'm not understanding your point about why comparing two languages like this is ridiculous. Java has a full blown runtime reflection system (it's "types exist at runtime"). JS doesn't. So Java wins in the reflection category.
- tracker1 8y agoBut in JS, reflection is pretty much not needed... I don't need to use reflection to see if an object has a quack method, or that I call it with the right types... I just call instance.quack() ... It's up to you as the developer to keep your interfaces and composition in line. It's actually WAY easier than with C# or Java. Since the use-case of reflection itself is largely unnecessary.
- lopatin 8y agoCalling a method and seeing what happens is not a substitute for reflection. You can do that in Java too btw.