7 ms·
Do you have any concrete example for "For example, in java, scanning your classes and their annotations to generate code for them at compile time is a standardi
by kangoo1707 8y ago
Do you have any concrete example for "For example, in java, scanning your classes and their annotations to generate code for them at compile time is a standardized feature"?
Tip: in JS, I don't want OOP class. If you really want class, could you show me the intention behind it?
- buerkle 8y agohttp://immutables.github.io/ http://immutables.github.io/ makes great use of this feature in Java
- gwright 8y agoIn general this what java annotation processors are all about. As a specific example, Dagger, a compile time, type safe dependency injection framework. https://google.github.io/dagger/ https://google.github.io/dagger/
- gwright 8y agoRetrofit is another example: https://square.github.io/retrofit/ https://square.github.io/retrofit/
- spricket 8y agoJava only works in classes :) . They're actually the compilation unit as well so no way around it. (Files are compiled a .class file at a time from single source files) 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.
- stickfigure 8y agoEvery popular ORM uses reflection to determine how to map fields to columns. For example, what type of date or money object to use. I automagically map some postgres JSONB columns to a parsed object form. Looks just like a regular typed nested object.
- hajile 8y agoThat's a solution looking for a problem in JS. You're thinking in types instead of in terms of a dynamic language. 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).
- stickfigure 8y agoyou can look everything over at runtime Correct, that's what you have to do in dynamic languages - look at what came in from the database and manually validate+convert it into the format you actually want. Also with pretty much every other form of input (JSON bodies, form posts, queue messages, etc). It's a lot of tedium. When working with node/python/ruby, I often find myself wishing I could just declare a type and be assured that "the system" will get it right instead of me having to code it out myself. You know, like Java. Well-written Java programs are generally more concise and less verbose than well-written JS/Python/Ruby programs. Which is to say, when programmers actually validate input instead of (ahem node) crashing the process and dropping all inflight connections when someone submits a json body missing an expected field. BTW absolutely nothing prevents you from building Java apps by declaring everything as Map<String, Object>. Nobody does that because it's a horrible way to program.
- joshuamorton 8y ago> Well-written Java programs are generally more concise and less verbose than well-written JS/Python/Ruby programs. This isn't at all true. I can say this from experience using statically typed Python extensively, in an average code base, 80% of your functions, if not more, are already statically correct in Python, one just need add an annotation. Then you're just as "well written" as the Java. With or without the annotations, the code is still just as "correct". > BTW absolutely nothing prevents you from building Java apps by declaring everything as Map<String, Object>. Nobody does that because it's a horrible way to program Yes, but only because you'd need to explicitly cast things everywhere and write out declarations everywhere. If you could always elide casts and type defs, it becomes a lot less horrible. > Correct, that's what you have to do in dynamic languages - look at what came in from the database and manually validate+convert it into the format you actually want. You still have to do this in static languages. Maybe a library does it for you, and it gives your an error or something you know is a DbRecordFieldStream or whatever, but dynamic languages can do the same thing. Orms validate input in dynamic languages too. Rails and Django can validate request formats. Protobufs work in every language.