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
33
votes
5 answers

Java in operator

For the one millionth time, I would have liked to use an IN operator in Java, similar to the IN operator in SQL. It could just be implemented as compiler syntactic sugar. So this if (value in (a, b, c)) { } else if (value in (d, e)) { } ...would…
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
33
votes
10 answers

Syntactic sugar in C/C++

I have been looking into Ruby and find its keywords "until" and "unless" very interesting. So I thought what was a good way to add similar keywords into C/C++. This is what I came up with: #define until(x) while(!(x)) #define unless(x) …
BiGYaN
  • 6,974
  • 5
  • 30
  • 43
32
votes
6 answers

Getting the desugared part of a Scala for/comprehension expression?

Does anyone know how to get the (Scala part only) desugared translation of a for/comprehension expression before it actually tries to compile in the REPL (or compiler)? The only thing I've found so far is the compiler "-print" flag but that gives…
IODEV
  • 1,706
  • 2
  • 17
  • 20
29
votes
2 answers

What does extended slice syntax actually do for negative steps?

The extended slice syntax in python has been explained to me as "a[n:m:k] returns every kth element from n to m". This gives me a good idea what to expect when k is positive. But I'm lost on how to interpret a[n:m:k] for negative k. I know that…
Alex Becker
  • 433
  • 1
  • 4
  • 10
28
votes
1 answer

Defining new infix operators

So thanks to C++11, it's now possible to combine macros, user-defined literals, lambdas, etc. to create the closest I can get to 'syntactic sugar'. An example would be if (A contains B) Of course this is easy. cout <<("hello"_s contains…
user1508519
28
votes
2 answers

Python decorators just syntactic sugar?

Possible Duplicate: Understanding Python decorators I am quite new on using Python decorators and from what I understand on my first impression that they are just syntactic sugar. Is there a more profound use of them for more complex uses ?
coredump
  • 3,017
  • 6
  • 35
  • 53
27
votes
14 answers

Elegant ways to return multiple values from a function

It seems like in most mainstream programming languages, returning multiple values from a function is an extremely awkward thing. The typical solutions are to make either a struct or a plain old data class and return that, or to pass at least some…
dsimcha
  • 67,514
  • 53
  • 213
  • 334
24
votes
13 answers

How useful is C#'s ?? operator?

So I have been intrigued by the ?? operator, but have still been unable to use it. I usually think about it when I am doing something like: var x = (someObject as someType).someMember; If someObject is valid and someMember is null, I could do var x…
captncraig
  • 22,118
  • 17
  • 108
  • 151
21
votes
2 answers

Ignore an element while building list in python

I need to build a list from a string in python using the [f(char) for char in string] syntax and I would like to be able to ignore (not insert in the list) the values of f(x) which are equal to None. How can I do that ?
Mat
  • 1,022
  • 2
  • 9
  • 24
21
votes
8 answers

Shorthand for for-loop - syntactic sugar in C++(11)

Actually these are two related questions. I know there is a new syntax in C++11 for range-based for loops of the form: //v is some container for (auto &i: v){ // Do something with i } First question: how can I infer at which iteration I am in…
dingalapadum
  • 2,077
  • 2
  • 21
  • 31
21
votes
3 answers

Scala single method interface implementation

Does Scala have any syntactic sugar to replace the following code: val thread = new Thread(new Runnable { def run() { println("hello world") } }) with something more like: val thread = new Thread(() => println("hello world")) in…
Mequrel
  • 727
  • 6
  • 12
21
votes
3 answers

Is there a way to shorten the C++11 lambda signature in declaration?

I want to shorten the following type of lambdas: [] (SomeVeryLongTemplateType, AnotherLongType) {}; Since the only reason for this lambda is to initialize some class std::function<...> member - it doesn't capture…
abyss.7
  • 13,882
  • 11
  • 56
  • 100
20
votes
6 answers

Is there a way to call multiple functions on the same object with one line?

Just trying to tidy up a program and was wondering if anyone could feed me some syntax sugar with regard to calling a member function on one queue multiple times on the same line. For example, changing: queue q; q.push(0); q.push(1); to…
user6836841
20
votes
3 answers

Is it possible to use a bracketing syntactic sugar for an applicative functor?

In McBride and Paterson's 'Applicative programming with effects' they introduce some lovely syntactic sugar for lifting a pure function: [| f x y z |] for f <$> x <*> y <*> z and I recall someone somewhere else using li f w x y z il or il f v w…
AndrewC
  • 32,300
  • 7
  • 79
  • 115
19
votes
2 answers

Using a block's return value in JavaScript

On a lot of browsers I've tested, JavaScript blocks actually return a value. You can test it out in any console: for(var i = 0; i < 10; i++) { var sqrt = Math.sqrt(i); if(Math.floor(sqrt) === sqrt) { i; } } The "return" value is…
Ry-
  • 218,210
  • 55
  • 464
  • 476
1
2
3
28 29