Java 8 上下文中的“糖”、“脱糖”术语是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22060894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:18:40  来源:igfitidea点击:

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

javasemanticsjava-8

提问by Xelian

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

我在 Java 8 中更常听到“糖化”和“去糖化”,这些术语是什么意思?它们是概念上的还是句法上的。

Some Example:

一些例子:

Default iterated loop resugaring to java

Observations about syntactic sugar in compilation.

默认迭代循环重新添加到 java

关于编译中的语法糖的观察。

采纳答案by Denys Séguret

sugar, in programming, usually refers to those sweetadditions, 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 sugarbut 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)
  • 后缀和前缀增量运算符(i++++i)。它们的唯一目的是避免编写额外的语句。它们是纯糖。
  • +=, |=, &=, 等是由同一种糖制成的。
  • 原始类型和对象之间的隐式转换也是糖。
  • 类型推断也是糖。
  • Java 8 附带的 Lambda 表达式是另一种糖(这不仅仅是语法上的

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.

人们普遍认为 Java 不够简洁,尤其是与现代语言相比。这就是为什么那些有助于加快代码阅读速度的添加内容受到欢迎。

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

最后,我只想指出,虽然缺乏糖分会使您的程序变胖,但过多的糖分会导致编写相同内容的多种不同方式,会使您的语言变得不舒服,并且您的程序不那么连贯且更难维护. 另一种糖,API 糖,通常是一种瘟疫,它使 API 更难掌握,尤其是当它由添加物(例如超载)组成时。

This being said, desugaringrefers 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)
  • 删除语言中所有冗余的过程
  • 代码处理器找出加糖语句背后的内容的过程(例如,这可能涉及类型推断)

回答by Shorn

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

“脱糖”在 Java 8 中似乎具有非常具体的含义。它似乎是一个笼统的术语,用于表达 lambda 表达式可能绑定到实际具体方法调用的各种方式。

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

如果您对具体细节感兴趣,这份关于“Lambda 表达式的翻译”的文档似乎包含正在发生的事情的真实细节。

A key phrase from the document:

文件中的一个关键短语:

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

将 lambda 转换为字节码的第一步是将 lambda 主体脱糖为方法。

回答by Vicente Romero

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:

一般来说,javac 中的“脱糖”允许用预先存在的语言特性表示一些语言特性。这允许在字节码中表示它们,而无需对类文件格式进行大的更改。同样出于这个原因,编译器的后端比前端更稳定。这并不意味着每个新的语言功能都只是语法糖,lambda 和方法引用绝对不是这种情况。编译器中还有更多“脱糖”的例子:

  • 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
  • for 每个循环都被“脱糖”为 C 风格的 for 循环
  • 断言被“脱糖”到 if 语句中
  • 内部类表示为一个独立的类

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

您还可以调查 String 开关、类型擦除、...