Syntactic sugar

from Wikipedia, the free encyclopedia

Syntactic sugar are syntax extensions in programming languages , which serve to simplify spellings. These extensions are alternative spellings, but they do not extend the expressiveness and functionality of the programming language.

Syntactic sugar can be traced back to basic elements of language through pure text transformations (“desugar”, dt. To sweeten).

The term syntactic sugar was coined in the 1960s by the British computer scientist Peter J. Landin .

Examples

Syntactic sugar in C

An example of syntactic sugar is the handling of fields in the C programming language . Strictly speaking, C does not distinguish between pointers to objects and pointers to fields of objects. If the variable is of pthe type “pointer to byte ” (type char *), you can use to *(p+3)access the third byte in the memory after the address p. This can also be written briefly in C as p[3].

Another example of syntactic sugar is the infix notation . In the infix notation, the operator is between the operands, e.g. B. 3 + 5. A translator can add(3,5)transfer this directly into the classic notation of a function call.

Diamond operator in Java

An example of syntactic sugar in Java is the so-called "diamond operator" <>. This was introduced with version 7. It forwards the generic type of the object to be created based on the type specification of the reference.

Instead of

List<HashMap<String, Point>> list = new ArrayList<HashMap<String, Point>>();

it is now enough to write

List<HashMap<String, Point>> list = new ArrayList<>();

to create one of ArrayListthe characteristics ArrayList<HashMap<String, Point>>.

do notation in Haskell

In the functional programming language Haskell , so-called monads are used for many purposes, but especially for input and output . For example, to read in a line and a letter from the standard input, to append the letter to the front of the line and to output the result again, one would have to write

getLine >>= \s -> getChar >>= \c -> putStrLn (c:s)

Better wrapping results in:

      getLine >>=
\s -> getChar >>=
\c -> putStrLn (c:s)

Since such constructs are required very often, the so-called do notation was introduced. The following code is exactly equivalent to the example above:

do
    s <- getLine
    c <- getChar
    putStrLn (c:s)

This form is very reminiscent of an imperative program and makes it easier to understand the content.

Syntactic salt

The counterpart to syntactic sugar is syntactic salt  - a language property that makes it difficult to write bad or difficult-to-read codes without expanding functionality.

Individual evidence

  1. RWTH Aachen: syntactic sugar. Retrieved October 9, 2018 .
  2. ^ A b c Type-sound Syntactic Language Extension. (PDF) Retrieved October 9, 2018 (English).
  3. Edsger W. Dijkstra points out in his trip report  ( page no longer available , search in web archives ) that the term can be traced back to Peter Landin, and the term is already used in the 1965 report of the Department of Computer Science at the University of Illinois .@1@ 2Template: dead link / userweb.cs.utexas.edu
  4. Syntactic Sugar in C: Arrays. November 28, 2013, accessed October 9, 2018 .
  5. RWTH Aachen: syntactic salt. Retrieved October 9, 2018 .