4 ms·
My favorite usage for decorators in java is for defining http requests, it lets you write stuff like this @GET("/users/{id}") function loadUser(id: int
by seanalltogether 4y ago
My favorite usage for decorators in java is for defining http requests, it lets you write stuff like this
@GET("/users/{id}")
function loadUser(id: int): Promise<User> {}
@PUT("/users/{id}")
function updateUser(id: int, user: User): Promise<User> {}
- imbnwa 4y agoDid't know Java had a `Promise` type
- tlarkworthy 4y agoIt has an equivalent but that code is typescript
- divs1210 4y agoI didn't know Java had a `function` keyword. That is TS, not Java. Not sure why the gp is calling it Java.
- Jcowell 4y agoThey’re not . They’re saying it allows stuff like what’s it’s Java and the decorators in the TS code is exactly what it’s like in Java.
- nobleach 4y agoThat's not Java. I believe they're demoing in Typescript a pattern they like from Java/Spring/Quarkus/Micronaut.
- imbnwa 4y agoAh yikes, read that too quickly
- topspin 4y agoCan't fathom why annotation are used for this, except maybe as a holdover from years ago when Java didn't have certain language features. Other approaches in Java are much nicer and involve zero annotations. Example (from the Spark Java microframework documentation): path("/api", () -> { before("/*", (q, a) -> log.info("Received api call")); path("/email", () -> { post("/add", EmailApi.addEmail); put("/change", EmailApi.changeEmail); delete("/remove", EmailApi.deleteEmail); }); path("/username", () -> { post("/add", UserApi.addUsername); put("/change", UserApi.changeUsername); delete("/remove", UserApi.deleteUsername); }); }); All your endpoints concisely and composibly mapped out in code, and not smeared across dozens or hundreds of methods and classes. Not an annotation in sight; just plain old Java.
- seanalltogether 4y agoThe code I posted above is used for client side apps, not server side. Client code typically has to accommodate calling different systems that use different patterns so having more flexibility in mapping endpoints to results is more important.
- syspec 4y agoThat looks worse. Shorter code is not always better code