Fat comma

Short description: Syntactic construction in computer programming

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. Newer usage refers to the "`=>`" operator present in some programming languages. It is primarily associated with PHP, Ruby and Perl programming languages, 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 Perl.[1] In CoffeeScript and TypeScript, the fat comma is used to declare a function that is bound to `this`.[2][3]

# a typical, idiomatic use of the fat comma in Perl
my %hash = (
first_name => "Larry",
last_name  => "Wall",

## Contents

* 1 Subtleties
* 1.1 ALGOL 60
* 1.2 Perl
* 1.3 PHP
* 1.4 Ruby
* 2 Use as lambda functions
* 3 References

## Subtleties

### ALGOL 60

The ALGOL "fat comma" is semantically identical to the comma.[4] 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.[5]

Thus, where this would produce a run-time error under strict (barewords are not allowed):

�d_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 in a hash key assignment. The result is easier-to-read code, with a stronger emphasis on the name-value pairing of associative arrays.

### 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, the fat comma is the token to create hashes. Ruby 1.9 introduced a special syntax to use symbols as barewords.[6][7] In Ruby, the fat comma is called a hash rocket.[7]

# Old syntax
old_hash = { :name => 'Ruby', :influences => ['Perl', 'Python', 'Smalltalk'] }

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

## Use as lambda functions

The fat arrow is used to declare single expression anonymous functions in JavaScript,[8] and C sharp.[9]

## References

1. ↑ Conway, Damian (2005). "4: Values and Expressions". in Allison Randal and Tatiana Appandi. Perl Best Practices. O'Reilly Media, Inc.. pp. 66. ISBN 0-596-00173-8. "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."
2. ↑ Ashkenas, Jeremy. "Coffeescript Documentation: grammar.coffee". http://coffeescript.org/documentation/docs/grammar.html.
3. ↑ "Handbook – Functions". http://www.typescriptlang.org/docs/handbook/functions.html.
4. ↑ Revised Report on the Algorithmic Language Algol 60 by Peter Naur, et al.
5. ↑ perldoc.perl.org – perlop – Comma Operator
6. ↑ Galero, Michael. "Ruby 1.9 Hash in Ruby 1.8". http://devblog.michaelgalero.com/2008/04/03/ruby-19-hash-in-ruby-18/. Retrieved 3 April 2008.
7. ↑ 7.0 7.1 Nash, Phil. "I don't like the Ruby 1.9 hash syntax". Logical Friday. Archived from the original on 25 June 2011. https://web.archive.org/web/20110625034400/http://logicalfriday.com/2011/06/20/i-dont-like-the-ruby-1-9-hash-syntax/. Retrieved 13 July 2011.
8. ↑ "Fat arrows in javascript". https://dzone.com/articles/javascript-fat-city.
9. ↑ "Hacking Sharp Lambda Expressions into Hash Rockets". 20 July 2013. https://joelholder.com/2013/07/19/hacking-cs-lambda-expressions-into-hash-rockets/.

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Fat comma. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Fat_comma&oldid=4155141"