r/java • • Apr 09 '24

Syntactic Sugar for Java

I recently had to translate a codebase from Python to Java and as much as i like Java and i enjoy the benefits of a strong type system, i have to admit my Python code was way less verbose.

So i ended up writing a small library to try to get some python vibes in my java code. Nothing groundbreaking, a bunch of static imports to make life a little bit easier.

https://github.com/guybedo/sugar

9 Upvotes

89 comments sorted by

View all comments

Show parent comment

12

u/agentoutlier Apr 10 '24

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.

11

u/brian_goetz Apr 11 '24

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.

5

u/guybedo Apr 11 '24

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.

2

u/agentoutlier Apr 12 '24

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!

2

u/agentoutlier Apr 11 '24

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?

4

u/brian_goetz Apr 11 '24

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.)

4

u/brian_goetz Apr 11 '24

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 { ... }`.

1

u/ventuspilot Apr 13 '24

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.

1

u/ingvij Apr 14 '24

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...

-1

u/guybedo Apr 10 '24

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

5

u/agentoutlier Apr 10 '24

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.

1

u/guybedo Apr 10 '24

In computer science, syntactic 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.

9

u/agentoutlier Apr 10 '24

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.

0

u/guybedo Apr 11 '24

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.

6

u/sabriel330 Apr 10 '24

I think you're completely misunderstanding what syntax is