语法糖与特征
在 C#(和 Java)中,字符串只不过是一个具有存储长度和附加的一些方法的 char 数组。同样,(除了引用与值之外)对象只不过是添加了继承和接口的美化结构。
一方面,这些新增功能本身就像是清晰的功能和增强功能。在另一个层面上,它们感觉像是“语法糖”地位的微小升级。
为了进一步阐述这个想法,请考虑(我可能有一些细节错误,但要点仍然存在):
transistor
elementary logic gate
compound gate
| |
ALU flip-flop
| | |
| register RAM
| |
CPU
microcode
assembly
C
C++
| |
MSIL JavaScript
C# jQuery
很多时候,任何单层抽象看起来很像语法糖,但多层分离感觉彼此非常疏远。
你怎么知道什么时候某些东西不再是语法糖而开始成为真正的功能?
In C# (and Java) a string is little more than a char array with a stored length and a few methods tacked on. Likewise, (reference vs. value stuff aside) objects are little more than glorified structs with inheritance and interfaces added.
On one level, these additions feel like clear features and enhancements unto themselves. On another level, they feel like a marginal upgrade from the status of "syntactic sugar."
To take this idea further, consider (I may have some details wrong, but the point remains):
transistor
elementary logic gate
compound gate
| |
ALU flip-flop
| | |
| register RAM
| |
CPU
microcode
assembly
C
C++
| |
MSIL JavaScript
C# jQuery
Many times, any single layer of abstraction looks a lot like syntactic sugar but multiple layers of separation feel very removed from each other.
How do you know when something has stopped being syntactic sugar and started being a bona fide feature?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
当它暗示着不同的思维方式时,它被证明是一个特征而不是语法糖。
当你说对象实际上是具有方法和继承的美化结构时,你是对的。然而,这只是实现细节。物体允许以不同的方式思考。在思考对象时,您可以更轻松地与现实世界的实体联系起来。当我们从使用 go-to 跳到过程式编程时,同样的事情也发生了。在幕后,处理器仍然不断地从 OP 跳转到 OP,但我们可以以不同的、更黑盒的方式思考。
话虽如此,在极端情况下,你可以说一切都是语法糖,但其中一些糖是一种功能,当它允许你以不同的方式思考时。
It turns out to be a feature instead of syntactic sugar when it implies a different way of thinking.
You are right when you say objects are in fact glorified structs with methods and inheritance. That, however, is just the implementation detail. What objects allow is to think in a different way. You can relate more easily to real world entities when thinking about objects. The same thing happened when even further back in time, we jumped from using go-to's to procedural programming. Under the hood, the processor still keeps on jmp'ing from OP to OP, but we could think in a different, more black-box, way.
Having said that, in extreme, you can say everything is syntactic sugar, but some of that sugar is a feature when it allows you to think differently.
语法糖是一个功能。
Syntactic sugar is a feature.
所有软件都是构建在其他抽象之上的巨大抽象堆栈。字符串可能只不过是一个字符数组,但是有许多操作在字符串上感觉很自然,但在字符数组上却很尴尬。所有这些抽象的目标都是相同的:删除不相关的细节,以便开发人员可以专注于问题的重要部分。
正如您所指出的,所有现代编程语言都可以被淘汰,我们可以重新使用汇编语言。但我们的生产力将会直线下降。
我猜想,当人们觉得自己从某个东西中获益甚少时,就会称其为“语法糖”;而当他们觉得能从中获益匪浅时,则将其称为“功能”。这使得这种区别非常模糊,而且非常主观。
All of software is a giant stack of abstractions built on top of other abstractions. A string may be nothing more than an array of characters, but there are many operations that feel natural on strings, but awkward on character arrays. The goal of all of these abstractions is the same: remove irrelevant details so that the developer can focus on the important parts of the problem.
As you point out, all modern programming languages could be eliminated, and we could go back to working in assembly language. But our productivity would plummet.
I guess people call something syntactic sugar when they feel they get little benefit from it, and a feature when they feel the get a large benefit from it. That makes the distinction very fuzzy, and quite subjective.
变革何时提供价值?我已经用汇编程序编码了。我切换到 C 并查看编译器的输出。它的代码比我手工编写的汇编程序好 95% 以上,而且更容易编写。对我来说这提供了价值,所以我想说它不是糖。
C++ 帮助我将面向对象的思想转化为代码。只要开销不是太高,我认为它就是一个功能。
我是一个务实的人。我的回答是“如果我能看到它就有价值”
When the change provides value? I have coded in assembler. I switched to C and looked at the output from the compiler. It's code was 95+% as good as my hand crafted assembler and it was much easier to write. For me that provided value so I'd say it wasn't sugar.
C++ helps me translate my object oriented thoughts into code. As long as the overhead isn't terribly high then I think it's a feature.
I'm a practical sort. "If I can see it's valuable" is my answer
“语法糖”是你不喜欢的功能
"Syntactic sugar" is a feature you don't like
语法糖似乎是一种不会改变语言能力的语法,并且使用不同的构造可以完成完全相同的事情。字符串(在 Java 中思考)不仅仅是字符数组的语法糖。字符数组是可变的(如果不是长度,则内容是可变的)。如果没有 String 数组,则无法使用现有语言功能使 char 数组变得不可变。
另一方面,作用于字符串的加号运算符确实是使用 StringBuilder 和调用追加的语法糖。
It seems that syntactical surgar is a syntax that changes nothing about the abilities of the language, and using a different construct accomplishes exactly the same thing. A String (thinking in Java) is not just syntatical sugar over a char array. A char array is mutable (in content if not in length). You could not make a char array immutable with an existing language feature without a String array.
On the other hand, the plus operator working on Strings is indeed syntatical sugar for using a StringBuilder and calling append.
我不得不说,仅通过编写不同的代码并具有与使用语法糖相同类型的“时间约束”,就无法获得相同的结果。
我的示例是 Lambda 表达式,编写
foreach
循环并不需要花费太多精力,但使用.Foreach()
当然也不错;而不是自己重写整个HttpRequest
类。一是句法,一是特征。两者都节省时间,其中一个比另一个节省更多时间。I would have to say when the same result is cannot be achieved simply by writing different code, with the same type of "time-constraint" as using the syntactical sugar.
My Example would be a Lambda expression, writing a
foreach
loop doesn't take a lot of effort, but using.Foreach()
sure is nice too; versus rewriting the wholeHttpRequest
class on your own. One is syntactical, one is a feature. Both save time, one in a much bigger way than the other.一般来说,术语“语法糖”指的是语言功能,它永远不允许程序员做以前无法完成的事情,而是提供了一种很好的方法来表达已经可以用语言表达的事情,即使有些尴尬。
某些结构可以明确地被视为语法糖。例如,在 VB.NET 中,测试两个引用是否不相等的代码需要
If Not (ref1 Is Ref2)
,但较新版本的语言允许If ref1 IsNot Ref2
。新语法无法表达旧语法无法表达的内容,但新语法更清晰,不会引入歧义,不使用它的唯一原因是代码必须与旧版本向后兼容语言的。有些结构可能有点难以定义为糖。特别是,如果一种语言添加了与其他类型一起使用时与现有构造相同的构造,但与其他类型一起使用时将无法编译,则此类构造可以提供以前不存在的编译时类型验证方法。 Java 泛型通常可以从这个角度来看待。可以将添加的是在编译时拒绝
Cat
添加到ArrayList
中,就像添加ArrayList
一样简单; ArrayListDog
的保护。由于编译时约束不允许人们编写任何没有它们就无法编写的程序,因此有些人可能将它们视为语法糖。另一方面,即使类型验证是在编译时而不是运行时执行的,它仍然可能被视为程序的工作之一。Generally the term "syntactic sugar" refers to language features which never allowed a programmer to do something which could not be done before, but rather provided a nice means of expressing something that could already expressed in the language, even if somewhat more awkwardly.
Certain constructs may be unambiguously regarded as syntactic sugar. For example, in VB.NET, code to test for whether two references weren't equal used to require
If Not (ref1 Is Ref2)
but newer versions of the language allowIf ref1 IsNot Ref2
. Nothing can be expressed in the new syntax that couldn't be expressed in the old, but the new syntax is cleaner, introduces no ambiguities, and the only reason not to use it would be if code had to be back-compatible with old versions of the language.Some constructs may be a bit harder to define as sugar. In particular, if a language adds constructs which will work identically to existing construct when used with other types, but will fail compilation with others, such constructs may provide a means of compile-time type verification which did not exist previously. Java generics may generally be viewed in this light. One can add a
Cat
to anArrayList<Cat>
just as easily as to anArrayList
; what theArrayList<Cat>
adds is a guard to rejectDog
s at compile time. Since compile-time constraints don't allow one to write any program that couldn't be written without them, some people may view them as syntactic sugar. On the other hand, even though type verification is performed at compile-time rather than run-time, it might still be viewed as one of the jobs of a program.语法糖和语言特征基本上描述的是同一件事,即使语法糖有时以贬义的方式使用,而特征通常与语言架构中更深层次的变化相关联(引入 lambda 等)。
但这种区别很大程度上取决于个人观点(及其主观感受的有用性)。
关于语言设计方面以及您使用
strings
和char-arrays
的示例,我想说这既不应该是功能也不应该是糖,而只是可以用语言基本语法来表达(LOP - 面向语言的编程)。通用概念(类型类、元编程等)允许您自己表达许多新的有用的构造,而无需等待语言获得新功能。只要看看 Haskell 或 C++ 的元编程能力就知道了。Syntactic sugar and language feature are basically describing the same thing, even if syntactic sugar is sometimes used in a pejorative way whereas feature is often associated with deeper changes in the language architecture (introducing lambdas etc.).
But this distinction is very dependent on a individual point of view (and its subjectively felt usefulness).
Regarding language-design aspects and your example with
strings
andchar-arrays
, I would say that this should be neither a feature nor sugar, but simply expressible in the languages basic syntax (LOP - language-oriented programming). Generic concepts (typeclasses, metaprogramming etc.) allow you to express many new and useful constructs by yourself without waiting for the language to get a new feature. Just look at Haskell or C++'s metaprogramming capabilities.