Difference between revisions of "Syntactic sugar"

From Mario Fan Games Galaxy Wiki
m
m
Line 1: Line 1:
 
'''Syntactic sugar''' is a term used to describe alternate ways of performing the same task using different [[syntax]]. This is a common way of making programming languages easier to read or making some tasks shorter to type.
 
'''Syntactic sugar''' is a term used to describe alternate ways of performing the same task using different [[syntax]]. This is a common way of making programming languages easier to read or making some tasks shorter to type.
  
A common sugar seen in many languages involves accessing a value stored in an [[associative array]]. Often called ''dot syntax'', what would normally be typed as ''array["key"]'' can be typed as ''array.key'' instead. For example, [[Lua]] uses ''colon syntax'' for [[Function|method]]s, a dot syntax sugar that makes ''var:function(params)'' equivalent to ''var.function(self, params)''.
+
A common sugar seen in many languages involves accessing a value stored in an [[associative array]]. Often called ''dot syntax'', what would normally be typed as ''array["key"]'' can be typed as ''array.key'' instead. For example, [[Lua]] uses ''colon syntax'' for [[Function|method]]s, a dot syntax sugar that makes ''var:function(params)'' equivalent to ''var.function(var, params)''. In a more extreme form, [[C]] and [[C++]] use the square-bracket array notation to stand in for a pointer arithmetic expression: ''a[b]'' means ''*(a+b)''; incidentally this means ''b[a]'' is also another possibility.
  
 
[[Operators]] are also commonly provided sugars in many languages, for example, ''x += i'' is often the equivalent of ''x = x + i''.
 
[[Operators]] are also commonly provided sugars in many languages, for example, ''x += i'' is often the equivalent of ''x = x + i''.
  
 
[[Category:Fangame Terms]]
 
[[Category:Fangame Terms]]

Revision as of 02:09, 15 May 2009

Syntactic sugar is a term used to describe alternate ways of performing the same task using different syntax. This is a common way of making programming languages easier to read or making some tasks shorter to type.

A common sugar seen in many languages involves accessing a value stored in an associative array. Often called dot syntax, what would normally be typed as array["key"] can be typed as array.key instead. For example, Lua uses colon syntax for methods, a dot syntax sugar that makes var:function(params) equivalent to var.function(var, params). In a more extreme form, C and C++ use the square-bracket array notation to stand in for a pointer arithmetic expression: a[b] means *(a+b); incidentally this means b[a] is also another possibility.

Operators are also commonly provided sugars in many languages, for example, x += i is often the equivalent of x = x + i.