问题描述
我在Java 8中经常听说'糖化'和'贬低',这些术语是什么意思?它们是概念性的还是语法式的。
I hear about 'sugaring' and 'desugaring' more often in Java 8, what does these terms mean ? are they conceptual or syntactical.
一些例子:
关于编译中语法糖的观察。
Observations about syntactic sugar in compilation.
推荐答案
sugar ,在编程中,通常是指那些 sweet 添加,主要是快捷方式,使一些构造更容易打字和阅读(后者是,在实践中,在程序的生命周期中最重要的)。
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).
以下是一些例子:
- 后缀和前缀增量运算符(
i ++
和++ i
)。他们唯一的目的是避免写一个额外的声明。它们是纯糖。 -
+ =
,| =
,& =
等由相同种类的糖组成。 - 原始类型和对象之间的隐式转换也是糖。
- 类型推断也是糖。
- Lambda表达式,附带Java 8,是另一种糖(这不仅仅是语法)
- 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不够简洁,特别是与现代语言相比。这就是为什么欢迎那些有助于使代码更快阅读的增加的原因。
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.
为了完成,我只是注意到,虽然缺糖会使你的程序变胖,过量的糖,导致许多不同的方式来写相同的东西,可以使你的语言不安,你的程序不那么连贯,更难维护。另一种糖,API糖,通常是一种瘟疫,使API更难掌握,特别是当它由添加物(例如重载)组成时。
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).
这是说, desugaring 指的是
- 删除所有语言冗余的过程
- 代码处理器找出含糖声明后面的内容的过程(这可能涉及类型推断)
这篇关于什么是“糖”,“desugar”是什么? Java 8的上下文中的术语?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!