Fat comma
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 " # a typical, idiomatic use of the fat comma in Perl
my %hash = (
first_name => "Larry",
last_name => "Wall",
);
SubtletiesALGOL 60The 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)
PerlThe "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): %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 PHPIn 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"));
RubyIn 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 functionsThe fat arrow is used to declare single expression anonymous functions in JavaScript,[8] and C sharp.[9] References
|