# Fat comma

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Fat_comma
> Markdown URL: https://mediated.wiki/source/Fat_comma.md
> Source: https://en.wikipedia.org/wiki/Fat_comma
> Source revision: 1192849847
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Syntactic construction in computer programming}}
{{Use dmy dates|date=December 2023}}
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 "<code>)</code>''letters''<code>:(</code>" construction in [ALGOL 60](/source/ALGOL_60).  Newer usage refers to the "<code>=></code>" [operator](/source/Operator_(programming)) present in some [programming language](/source/programming_language)s. It is primarily associated with [PHP](/source/PHP), [Ruby](/source/Ruby_(programming_language)) and [Perl](/source/Perl) [programming language](/source/programming_language)s, which use it to declare [hashes](/source/Hash_table). Using a fat comma to bind key-value pairs in a hash, instead of using a comma, is considered an example of good [idiomatic](/source/Programming_idiom) Perl.<ref name="hashbind">{{Cite book|last=Conway|first=Damian|authorlink=Damian Conway|editor=[Allison Randal](/source/Allison_Randal) and Tatiana Appandi|title=[Perl Best Practices](/source/Perl_Best_Practices)|year=2005|publisher=O'Reilly Media, Inc.|isbn=0-596-00173-8|chapter=4: Values and Expressions|pages=66|quote=Whenever you are creating a list of key/value or name/value pairs, use the "fat comma" (=>) to connect the keys to their corresponding values.}}</ref> In [CoffeeScript](/source/CoffeeScript) and [TypeScript](/source/TypeScript), the fat comma is used to declare a function that is bound to <code>this</code>.<ref name=coffeescript-grammar>{{cite web|last=Ashkenas|first=Jeremy|title=Coffeescript Documentation: grammar.coffee|url=http://coffeescript.org/documentation/docs/grammar.html|access-date=11 December 2011|archive-url=https://web.archive.org/web/20120104174350/http://coffeescript.org/documentation/docs/grammar.html|archive-date=4 January 2012|url-status=dead}}</ref><ref>{{Cite web|url=http://www.typescriptlang.org/docs/handbook/functions.html|title = Handbook – Functions}}</ref>

<syntaxhighlight lang="perl">
# a typical, idiomatic use of the fat comma in Perl
my %hash = (
    first_name => "Larry",
    last_name  => "Wall",
);
</syntaxhighlight>

== Subtleties ==

===ALGOL 60===
The ALGOL "fat comma" is semantically identical to the comma.<ref>[http://www.masswerk.at/algol60/report.htm Revised Report on the Algorithmic Language Algol 60] by Peter Naur, et al.</ref> 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:
<syntaxhighlight lang="text">
S(s-5, T, P)
S(s-5) t: (T) p: (P)
S(s-5) Temperature: (T) Pressure: (P)
</syntaxhighlight>

===Perl===
The "fat comma" forces the word to its left to be interpreted as a string.<ref>[http://perldoc.perl.org/perlop.html#Comma-Operator perldoc.perl.org – perlop – Comma Operator]</ref>

Thus, where this would produce a run-time error under strict (barewords are not allowed):
<syntaxhighlight lang="perl">
%bad_example = ( bad_bareword, "not so cool" );
</syntaxhighlight>
the following use of the fat comma would be legal and idiomatic:
<syntaxhighlight lang="perl">
%good_example = ( converted_to_string => "very monkish" );
</syntaxhighlight>
This is because the token <code>converted_to_string</code> would be converted to the string literal <code>"converted_to_string"</code> which is a legal [argument](/source/Parameter_(computer_science)) in a hash key assignment.
The result is easier-to-read code, with a stronger emphasis on the name-value pairing of [associative array](/source/associative_array)s.

===PHP===

In [PHP](/source/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:
<syntaxhighlight lang="php">
$array = array("name" => "PHP", "influences" => array("Perl", "C", "C++", "Java", "Tcl"));
</syntaxhighlight>

===Ruby===
In [Ruby](/source/Ruby_(programming_language)), the fat comma is the token to create hashes. Ruby 1.9 introduced a special syntax to use [symbols](/source/Symbol_(programming)) as barewords.<ref name=rubyhash>{{cite web|last=Galero|first=Michael|title=Ruby 1.9 Hash in Ruby 1.8|url=http://devblog.michaelgalero.com/2008/04/03/ruby-19-hash-in-ruby-18/|accessdate=3 April 2008}}</ref><ref name=rubyidontlike/> In [Ruby](/source/Ruby_(programming_language)), the fat comma is called a '''hash rocket'''.<ref name=rubyidontlike>{{cite web |last=Nash |first=Phil |title=I don't like the Ruby 1.9 hash syntax |url=http://logicalfriday.com/2011/06/20/i-dont-like-the-ruby-1-9-hash-syntax/ |work=Logical Friday |accessdate=13 July 2011 |url-status=dead |archiveurl=https://web.archive.org/web/20110625034400/http://logicalfriday.com/2011/06/20/i-dont-like-the-ruby-1-9-hash-syntax/ |archivedate=25 June 2011 }}</ref>
<syntaxhighlight lang="ruby">
# Old syntax
old_hash = { :name => 'Ruby', :influences => ['Perl', 'Python', 'Smalltalk'] }

# New syntax (Ruby >= 1.9)
new_hash = { name: 'Ruby', influences: ['Perl', 'Python', 'Smalltalk'] }
</syntaxhighlight>

== Use as lambda functions ==

The fat arrow is used to declare single [expression](/source/expression_(computer_programming)) [anonymous function](/source/anonymous_function)s in [JavaScript](/source/JavaScript),<ref>{{cite web|url=https://dzone.com/articles/javascript-fat-city|title=Fat arrows in javascript}}</ref> and [C sharp](/source/C_sharp_(computing)).<ref>{{cite web|url=https://joelholder.com/2013/07/19/hacking-cs-lambda-expressions-into-hash-rockets/|title=Hacking Sharp Lambda Expressions into Hash Rockets|date=20 July 2013}}</ref>

==References==
{{Reflist}}

Category:Perl
Category:Ruby (programming language)
Category:Articles with example Ruby code
Category:Programming constructs

---
Adapted from the Wikipedia article [Fat comma](https://en.wikipedia.org/wiki/Fat_comma) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Fat_comma?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
