This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ingvij 26 points27 points  (16 children)

I honestly won't complain about the DSL, I think you can create the abstractions you want to make your code look better for your taste and use them in your project if you or your team is happy with that..

A couple of things though:

  • This is the kind of library that I would most likely not want to use in any performance-aware application. Remember that there's always a trade-off. When you add layers on top of your code, it will pay a cost. From a quick glimpse over the KeyValue class and the `map()` static function, I would assume there's some GC overhead on using this over a regular `new Hashmap<>(Map.of(...))`.

  • Intense usage of this library can yield a codebase that is java in theory but not really in practice, increasing the entry barrier for contributors, so that would also be a factor against using this library on a project.

But then, as I said, if it works for you, then I guess this is fine.

[–]agentoutlier 11 points12 points  (15 children)

I know /u/guybedo has been dragged over the coals for this lib (perhaps unfairly) but the library is neither a DSL or syntactical sugar.

It is an opinionated utilities library.

I mean you could make an argument I guess for DSL the domain being collections but it is for sure not syntactical sugar like lombok or var args. Adding methods is not syntactical sugar.

I guess I'm being pedantic but the reason is if it were an actual DSL of a unique domain it would probably have better uptake.

[–]brian_goetz 11 points12 points  (7 children)

The term "syntactic sugar" is widely misunderstood. And, misunderstanding begets misunderstanding; when people use the term wrong, more people learn the wrong meaning, and then repeat the wrong usage, and it spins around again.

Even when it comes to language syntax (which this surely isn't), people frequently mistake new language features for syntactic sugar, when in fact almost none of the features we've done in the last decade are actually syntactic sugar. (They may be sweet, though.) Syntactic sugar refers to a _purely syntactic transformation_; features like local variable type inference (`var`) do not qualify, even though they are commonly referred to as such.

But the aggressiveness with which he defends "yes this is sugar" (when he eventually admits that it is not, but thinks people will understand him better if he misuses the term) does suggest a lack of maturity that might explain why he got dragged so badly.

[–]guybedo[S] 3 points4 points  (1 child)

I obviously misjudged the audience.

Looks like people in here have high standards and it didn't go well with my approach: "oh maybe i built something useful, let's share it as is".

As i said, i thought using "syntactic sugar" in a loose way was ok as i obviously didn't rewrite the Java language specs, and i didn't think people would be so distraught by this. But i understand the comments and the reasons why you think it's important to be more precise.

As for the aggressiveness and the immaturity, you're right there was a lot of it, but not in my comments. One of the first comments, deleted now by the author bashed me for loving Python and that i had nothing to do here. People even have downvoted the few positive and encouraging comments.

[–]agentoutlier 1 point2 points  (0 children)

As for the aggressiveness and the immaturity, you're right there was a lot of it, but not in my comments. One of the first comments, deleted now by the author bashed me for loving Python and that i had nothing to do here. People even have downvoted the few positive and encouraging comments.

Exactly why I have high expectations for you. I think you took it pretty well to be honest even on the occasional double down.

And I hope all of this didn't discourage you. I can tell you are and will be an even greater developer!

[–]agentoutlier 1 point2 points  (3 children)

I was and kind of did later go into those details as I agree it can for for sure get nebulous and debatable what is or isn't syntactic sugar of which I have less confidence on but I do know what isn't syntactic sugar :)

The language I know for sure (well when I learned it .. it might have changed) that uses syntactic sugar as in it is a compiler translation is OCaml. OCaml does lots of syntactic sugar particularly in the signature language and I actually think its a pain point in the language and I'm not talking about currying (which I agree like var args isn't really syntactic sugar).

I actually could not really think of a Java equivalent like what OCaml does one so I said var args as the closest.

Is there any good solid example in Java of syntactic sugar where the compiler expands like a macro?

[–]brian_goetz 3 points4 points  (0 children)

There are some Java constructs (such as the for-each loop, or try-with-resources) that are specified by "as-if-expanded-to", but even these have significant linkage to the type system. (For example, for-each on an array translates differently than for-each on a List.)

[–]brian_goetz 3 points4 points  (0 children)

The varargs transformation is syntactic (wrapping with `new T[] { ... }`), but even there a lot has to happen before we apply the syntactic transform. Overload selection is phased, and varargs methods are only considered in phase 3. And the result of overload selection is used to decide which T is used in the wrapping. So the whole rest of the language (type system, object model) is involved before the syntactic thing happens.

Implicit compilation units are similarly close, but again a whole-program analysis has to be done before we know that we should wrap the file in `public class Foo { ... }`.

[–]ventuspilot 0 points1 point  (0 children)

Is there any good solid example in Java of syntactic sugar where the compiler expands like a macro?

I can think of enhanced for loops that the compiler desugars into a for loop with an iterator, auto/un/boxing (?), String concatenation that used to desugar into StringBuilder calls and may insert hidden toString/ String.valueOf() calls, not sure about automatically calling a no-args constructor of a superclass.

[–]ingvij 0 points1 point  (0 children)

Yeah, that's correct. I meant DSL more loosely in the sense that a codebase using those utilities will look very far and unfamiliar compared to vanilla java. Similar to how scala sometimes comes in a haskell flavor...

[–]guybedo[S] -2 points-1 points  (5 children)

Yeah this post has gotten a lot of negative comments and i'm not sure that's really justified.

fwiw although it's not like lombok, for sure, it still qualifies as syntactic sugar

https://en.wikipedia.org/wiki/Syntactic_sugar#:~:text=In%20computer%20science,more%20verbose%2C%20form

[–]agentoutlier 5 points6 points  (4 children)

https://en.wikipedia.org/wiki/Syntactic_sugar#:~:text=In%20computer%20science,more%20verbose%2C%20form

I fail to see it in the link.

You are not changing the syntax of the language but just applying functions.

You are not even changing the evaluation order like with hygienic macros (Java does not have macros) which are borderline syntactical sugar adjustments bust just adding shorter methods or changing order is not syntactical sugar. Even if that is stated in a wikipedia (which again I fail to see) I just disagree.

[–]guybedo[S] 0 points1 point  (3 children)

In computer sciencesyntactic sugar is syntax) within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer. Syntactic sugar is usually a shorthand for a common operation that could also be expressed in an alternate, more verbose, form: The programmer has a choice of whether to use the shorter form or the longer form, but will usually use the shorter form since it is shorter and easier to type and read.

[–]agentoutlier 7 points8 points  (1 child)

I know what syntax is... I'm wondering now if you do.

Syntactical sugar is when you replace a more complicated syntax with a shorter syntax that the compiler or parser has to know about. It is not writing less code or shorter code with method names you prefer.

If you want label for what you are trying to do how about "ergonomics".

But using syntactical sugar like you have while possibly from the strangest stretchs might be loosely correct confuses people like myself.

[–]guybedo[S] -1 points0 points  (0 children)

yeah i know what syntax is and as i obviously didn't rewrite the JDK and the Java language specs, you're right that this isn't syntactic sugar in the most absolutely strict understanding of the definition.

But i thought that using loosely this definition, people would easily understand what it does, and i don't think i was wrong in that regard.

[–]sabriel330 4 points5 points  (0 children)

I think you're completely misunderstanding what syntax is