Fat comma
   HOME

TheInfoList



OR:

The fat comma (also termed hash rocket in Ruby and a fat arrow in JavaScript) is a syntactic construction that appears in a position in a function call (or definition) where a comma would usually appear. The original usage refers to the ")''letters'':(" construction in
ALGOL 60 ALGOL 60 (short for ''Algorithmic Language 1960'') is a member of the ALGOL family of computer programming languages. It followed on from ALGOL 58 which had introduced code blocks and the begin and end pairs for delimiting them, representing a ...
. Newer usage refers to the "=>" operator present in some
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
s. It is primarily associated with PHP,
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called ...
and
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offic ...
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
s, which use it to declare hashes. Using a fat comma to bind key-value pairs in a hash, instead of using a comma, is considered an example of good
idiomatic Idiom, also called idiomaticness or idiomaticity, is the syntactical, grammatical, or structural form peculiar to a language. Idiom is the realized structure of a language, as opposed to possible but unrealized structures that could have develop ...
Perl. In
CoffeeScript CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell in an effort to enhance JavaScript's brevity and readability. Specific additional features include list comprehe ...
and
TypeScript TypeScript is a free and open source programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript and adds optional static typing to the language. It is designed for the development of large app ...
, the fat comma is used to declare a function that is bound to this. # a typical, idiomatic use of the fat comma in Perl my %hash = ( first_name => "Larry", last_name => "Wall", );


Subtleties


ALGOL 60

The ALGOL "fat comma" is semantically identical to the comma. In particular, whether letter strings are used, and what their contents are, need not match between the definition of a function and its uses. The following are equivalent: S(s-5, T, P) S(s-5) t: (T) p: (P) S(s-5) Temperature: (T) Pressure: (P)


Perl

The "fat comma" forces the word to its left to be interpreted as a string. Thus, where this would produce a run-time error under strict (barewords are not allowed): %bad_example = ( bad_bareword, "not so cool" ); the following use of the fat comma would be legal and idiomatic: %good_example = ( converted_to_string => "very monkish" ); This is because the token converted_to_string would be converted to the string literal "converted_to_string" which is a legal
argument An argument is a statement or group of statements called premises intended to determine the degree of truth or acceptability of another statement called conclusion. Arguments can be studied from three main perspectives: the logical, the dialecti ...
in a hash key assignment. The result is easier-to-read code, with a stronger emphasis on the name-value pairing of
associative array In computer science, an associative array, map, symbol table, or dictionary is an abstract data type that stores a collection of (key, value) pairs, such that each possible key appears at most once in the collection. In mathematical terms an ...
s.


PHP

In PHP, the fat comma is termed a double arrow, and is used to specify key/value relationships when declaring an array. Unlike in Perl, the double arrow does not treat what comes before it as a bare word, but rather evaluates it. Hence, constants used with the double arrow will be evaluated: $array = array("name" => "PHP", "influences" => array("Perl", "C", "C++", "Java", "Tcl"));


Ruby

In
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called ...
, the fat comma is the token to create hashes. Ruby 1.9 introduced a special syntax to use
symbols A symbol is a mark, sign, or word that indicates, signifies, or is understood as representing an idea, object, or relationship. Symbols allow people to go beyond what is known or seen by creating linkages between otherwise very different co ...
as barewords. In
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called ...
, the fat comma is called a hash rocket. # Old syntax old_hash = # New syntax (Ruby >= 1.9) new_hash =


Use as lambda functions

The fat arrow is used to declare single
expression Expression may refer to: Linguistics * Expression (linguistics), a word, phrase, or sentence * Fixed expression, a form of words with a specific meaning * Idiom, a type of fixed expression * Metaphorical expression, a particular word, phrase, o ...
anonymous function In computer programming, an anonymous function (function literal, lambda abstraction, lambda function, lambda expression or block) is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed t ...
s in
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behavior, of ...
, and C sharp.


References

{{Reflist Perl Ruby (programming language) Articles with example Ruby code Programming constructs