What are “sugar”, “desugar” terms in context of Java 8?

橙三吉。 提交于 2019-12-03 01:45:35

问题


I hear about 'sugaring' and 'desugaring' more often in Java 8, what does these terms mean ? are they conceptual or syntactical.

Some Example:

Default iterated loop resugaring to java

Observations about syntactic sugar in compilation.


回答1:


sugar, in programming, usually refers to those sweet additions, mostly shortcuts, that make some constructs easier to type and to read (the latter being, in practice, the most important during the life cycle of your program).

Wikipedia has a definition of syntactic sugar but you should note that not all sugar is, in essence, syntactical (not all recent sweet additions were just compiler changes).

Here are a few examples :

  • the postfix and prefix increment operators (i++ and ++i). Their only purpose is to avoid writing an additional statement. They're pure sugar.
  • +=, |=, &=, etc. are made of the same kind of sugar.
  • Implicit conversion between primitive types and objects is sugar too.
  • type inference is sugar too.
  • Lambda expression, coming with Java 8, is some other kind of sugar (this one not just syntactical)

Java is widely seen as not being concise enough, especially compared to modern languages. That's why those additions that help make the code faster to read are welcome.

To finish, I'd just note that while a lack of sugar can make your program fat, an excess of sugar, leading to many different ways to write the same things, can make your language queasy and your program less coherent and harder to maintain. Another kind of sugar, API sugar, is most often a plague which makes the API harder to grasp, especially when it's made of additions (overloading for example).

This being said, desugaring refers either to

  • the process by which you remove all that is redundant in a language
  • the process by which a code processor finds out what's behind a sugared statement (this may for example involves type inference)



回答2:


"Desugaring" appears to have a very specific meaning in Java 8. It seems to be a catch-all term to express the various ways a lambda expression may be bound to an actual concrete method call.

This document on "Translation of Lambda Expressions" seems to have the real details of what's going on if you're interested in specifics.

A key phrase from the document:

The first step of translating lambdas into bytecode is desugaring the lambda body into a method.




回答3:


In general "desugaring" in javac allows representing some language features with preexisting ones. This allows representing them in the bytecode without making big changes to the class file format. Also for this reason the back-end of the compiler is more stable than the front-end. This doesn't mean that every new language feature is just syntactic sugar, as is definitely not the case of lambdas and method references. There are more examples of "desugaring" in the compiler:

  • for each loops are "desugared" to C style for loops
  • assertions are "desugared" to an if sentence
  • inner classes are represented as a standalone class

You can investigate also what happen with the String switch, type erasure,...



来源:https://stackoverflow.com/questions/22060894/what-are-sugar-desugar-terms-in-context-of-java-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!