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
19
votes
2 answers

Is it possible to roll your own syntax sugar (like do-notation, or arrow-notation) in Haskell?

Well, the question is self-explicative. Suppose I want to implement some special syntax just for fun. Is it possible? What tools should I use?
Rafael S. Calsaverini
  • 13,582
  • 19
  • 75
  • 132
19
votes
8 answers

Why would you use "AS" when aliasing a SQL table?

I just came across a SQL statement that uses AS to alias tables, like this: SELECT all, my, stuff FROM someTableName AS a INNER JOIN someOtherTableName AS b ON a.id = b.id What I'm used to seeing is: SELECT all, my, stuff FROM someTableName…
froadie
  • 79,995
  • 75
  • 166
  • 235
19
votes
4 answers

Java for each vs regular for -- are they equivalent?

Are these two constructs equivalent? char[] arr = new char[5]; for (char x : arr) { // code goes here } Compared to: char[] arr = new char[5]; for (int i = 0; i < arr.length; i++) { char x = arr[i]; // code goes here } That is, if I…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
19
votes
3 answers

Boolean assignment operators in PHP

I find myself doing this kind of thing somewhat often: $foo = true; $foo = $foo && false; // bool(false) With bitwise operators, you can use the &= and |= shorthand: $foo = 1; $foo &= 0; // int(0) Given that bitwise operations on 1 and 0 are…
FtDRbwLXw6
  • 27,774
  • 13
  • 70
  • 107
18
votes
5 answers

Assign a single value to multiple variables in one line in Rust?

A common way to assign multiple variables is often expressed in programming languages such as C or Python as: a = b = c = value; Is there an equivalent to this in Rust, or do you need to write it out? a = value; b = value; c = value; Apologies if…
ideasman42
  • 42,413
  • 44
  • 197
  • 320
18
votes
3 answers

AWK: shortened if-then-else with regex

The following AWK format: /REGEX/ {Action} Will execute Action if the current line matches REGEX. Is there a way to add an else clause, which will be executed if the current line does not matches the regex, without using if-then-else explicitly,…
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
17
votes
6 answers

How can I use collection initializer syntax with ExpandoObject?

I've noticed that the new ExpandoObject implements IDictionary which has the requisite IEnumerable> and Add(string, object) methods and so it should be possible to use the collection initialiser syntax to…
17
votes
5 answers

Is foreach purely “syntactic sugar”?

The compiler compiles a foreach loop into something like a for loop when the foreach is used with an array. And the compiler compiles a foreach loop into something like a while loop when the foreach is used with an IEnumerable or IEnumerable.…
Cui Pengfei 崔鹏飞
  • 8,017
  • 6
  • 46
  • 87
17
votes
3 answers

Evaluate many boolean expressions like Array#join in Ruby

In Ruby, you can use Array#join to simple join together multiple strings with an optional delimiter. [ "a", "b", "c" ].join #=> "abc" [ "a", "b", "c" ].join("-") #=> "a-b-c" I'm wondering if there is nice syntactic sugar to do something…
istrasci
  • 1,331
  • 1
  • 18
  • 40
16
votes
4 answers

Don't call function if None Value returned

suppose you have a function that can return some object or None: def foobar(arg): if arg == 'foo': return None else: return 'bar' Now you call this method and you want to do something with the object, for this example i get a…
reox
  • 5,036
  • 11
  • 53
  • 98
16
votes
7 answers

Python assert -- improved introspection of failure?

This is a rather useless assertion error; it does not tell the values of the expression involved (assume constants used are actually variable names): $ python -c "assert 6-(3*2)" [...] AssertionError Is there a better assert implementation in…
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
15
votes
5 answers

What kind of syntactic sugar is available in Perl to reduce code for l/rvalue operators vs. if statements?

There's a bunch out there, as Perl is a pretty sugary language, but the most used statements in any language is the combination of if statements and setting values. I think I've found many of them, but there's still a few gaps. Ultimately, the…
SineSwiper
  • 2,046
  • 2
  • 18
  • 22
15
votes
1 answer

Python-like dictionary declaration for C#?

In Python one can do: d = {1 : 'Hello', 2 : 'World'} In C# it's more verbose: Dictionary d = new Dictionary(); d.Add(1, 'Hello'); d.Add(2, 'World'); How can I make this less verbose?
Aillyn
  • 23,354
  • 24
  • 59
  • 84
15
votes
11 answers

Why is Syntactic Sugar sometimes considered a bad thing?

Syntactic sugar, IMHO, generally makes programs much more readable and easier to understand than coding from a very minimalistic set of primitives. I don't really see a downside to good, well thought out syntactic sugar. Why do some people…
dsimcha
  • 67,514
  • 53
  • 213
  • 334
15
votes
2 answers

How to unpack the columns of a pandas DataFrame to multiple variables

Lists or numpy arrays can be unpacked to multiple variables if the dimensions match. For a 3xN array, the following will work: import numpy as np a,b = [[1,2,3],[4,5,6]] a,b = np.array([[1,2,3],[4,5,6]]) # result: a=[1,2,3], …
normanius
  • 8,629
  • 7
  • 53
  • 83
1 2
3
28 29