Questions tagged [syntactic-sugar]

Syntactic sugar is a software development term that refers to changes in a programming language to make it more easily readable by programmers working with the language. Usually this means codifying idioms and abbreviating verbosity.

It makes the language "sweeter" for humans to use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer.

Specifically, a construct in a language is called syntactic sugar if it can be removed from the language without any effect on what the language can do: functionality and expressive power will remain the same. All applications of the construct can be systematically replaced with equivalents that do not use it. For instance, in imperative programming languages, for loops can be systematically replaced with while loops and iterators.

More generally, the term is used to characterize syntax as being designed for ease of expression. For instance, in C#, the property construct may be called syntactic sugar: it is roughly, but not exactly, equivalent to a getter-setter pair of methods against a private field.

435 questions
1715
votes
29 answers

How does the Java 'for each' loop work?

Consider: List someList = new ArrayList(); // add "monkey", "donkey", "skeleton key" to someList for (String item : someList) { System.out.println(item); } What would the equivalent for loop look like without using the for each…
Jay R.
  • 31,911
  • 17
  • 52
  • 61
181
votes
2 answers

Is there a literal notation for an array of symbols?

I like this literal expression for an array of strings: %w( i can easily create arrays of words ) I am wondering if there is a literal to get an array of symbols. I know I can do %w( it is less elegant to create arrays of symbols ).map( &:to_sym…
m_x
  • 12,357
  • 7
  • 46
  • 60
110
votes
5 answers

What is the purpose of "!" and "?" at the end of method names?

Sometimes I see methods in Ruby that have "?" and "!" at the end of them, e.g: name = "sample_string" name.reverse name.reverse! name.is_binary_data? I was wondering what their purpose is? Are they just syntax sugarcoating?
itsaboutcode
  • 24,525
  • 45
  • 110
  • 156
79
votes
3 answers

How does Scala's apply() method magic work?

In Scala, if I define a method called apply in a class or a top-level object, that method will be called whenever I append a pair a parentheses to an instance of that class, and put the appropriate arguments for apply() in between them. For…
Jeff
  • 14,831
  • 15
  • 49
  • 59
78
votes
4 answers

Python decorator best practice, using a class vs a function

As I've understood it there are two ways to do a Python decorator, to either use the __call__ of a class or to define and call a function as the decorator. What's the advantages/disadvantages of these methods? Is there one preferred method? Example…
olofom
  • 6,233
  • 11
  • 37
  • 50
73
votes
40 answers

What is the best or most interesting use of Extension Methods you've seen?

I'm starting to really love extension methods... I was wondering if anyone her has stumbled upon one that really blew their mind, or just found clever. An example I wrote today: Edited due to other users' comments: public static IEnumerable
Mark Carpenter
  • 17,445
  • 22
  • 96
  • 149
72
votes
9 answers

C# property and ref parameter, why no sugar?

I just ran across this error message while working in C# A property or indexer may not be passed as an out or ref parameter I known what caused this and did the quick solution of creating a local variable of the correct type, calling the function…
BCS
  • 75,627
  • 68
  • 187
  • 294
67
votes
6 answers

What are all the instances of syntactic sugar in Scala?

What are all the instances of syntactic sugar in Scala? They are hard to search for since most/all of them are purely symbols and are thus hard to search for without knowing the name of the concept. TODO: Implicit conversions _ syntax for…
Jackson Davis
  • 3,411
  • 2
  • 22
  • 21
60
votes
4 answers

Haskell record syntax

Haskell's record syntax is considered by many to be a wart on an otherwise elegant language, on account of its ugly syntax and namespace pollution. On the other hand it's often more useful than the position based alternative. Instead of a…
Rob Agar
  • 12,337
  • 5
  • 48
  • 63
58
votes
6 answers

Is there a way to implement custom language features in C#?

I've been puzzling about this for a while and I've looked around a bit, unable to find any discussion about the subject. Lets assume I wanted to implement a trivial example, like a new looping construct: do..until Written very similarly to…
Thebigcheeze
  • 3,408
  • 2
  • 22
  • 18
57
votes
16 answers

Magic First and Last Indicator in a Loop in Ruby/Rails?

Ruby/Rails does lots of cool stuff when it comes to sugar for basic things, and I think there's a very common scenario that I was wondering if anyone has done a helper or something similar for. a = Array.new(5, 1) a.each_with_index do |x, i| …
aronchick
  • 6,786
  • 9
  • 48
  • 75
51
votes
4 answers

Destructuring assignment - object properties to variables in C#

JavaScript has a nifty feature where you can assign several variables from properties in an object using one concise line. It's called destructuring assignment syntax which was added in ES6. // New object var o = {p1:'foo', p2:'bar', p3: 'baz'}; //…
styfle
  • 22,361
  • 27
  • 86
  • 128
49
votes
3 answers

Syntax sugar: _* for treating Seq as method parameters

I just noticed this construct somewhere on web: val list = List(someCollection: _*) What does _* mean? Is this a syntax sugar for some method call? What constraints should my custom class satisfy so that it can take advantage of this syntax sugar?
user7865221
  • 491
  • 1
  • 4
  • 3
39
votes
4 answers

java, is there a way we can import a class under another name

is there a way we can import a class under another name? Like if i have a class called javax.C and another class called java.C i can import javax.C under the name C1 and import java.C under the name C2. We can do something like this in C#: using…
Pacerier
  • 86,231
  • 106
  • 366
  • 634
36
votes
8 answers

WITH statement in Java

In VB.NET there is the WITH command that lets you omit an object name and only access the methods and properties needed. For example: With foo .bar() .reset(true) myVar = .getName() End With Is there any such syntax within Java? Thanks!
Mike Clark
  • 11,769
  • 6
  • 39
  • 43
1
2 3
28 29