6

In some languages, if a function returns an array, then as opposed to storing the array in a variable then retrieving a single element, like this:

var someValues = getSomeValues();
var firstElement = someValues[0];

You can use the array index notation directly after the function call to retrieve the element of the returned array instead, like this:

var firstElement = getSomeValues()[0];

What is this construct or syntax known as? Does it have a special name at all?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 1
    It's [Array Dereferencing](http://wiki.php.net/rfc/functionarraydereferencing) and a duplicate of [Access array element from function call in php](http://stackoverflow.com/questions/2282051/access-array-element-from-function-call-in-php) – Gordon Aug 25 '10 at 20:40
  • 1
    @Gordon: on second thought, I don't think it's really a duplicate since I'm asking what it's called while that question is asking why it doesn't work in PHP. But oh well, at least I know its name now. – BoltClock Aug 25 '10 at 21:19
  • @BoltClock true, but you get the answer to yours by reading the other question, so while it's semantically not a duplicate, it contains enough info to answer yours. – Gordon Aug 25 '10 at 21:34
  • 1
    @BoltClock Sorry to burst your bubble here, but: http://odata.stackexchange.com/stackoverflow/q/10631/. Not the first ;) – Yi Jiang Sep 08 '10 at 15:34

5 Answers5

3

You can use the array index notation directly on the function call to retrieve the element

var firstElement = getSomeValues()[0];

What you are doing is accessing the indexed element of the array/collection returned by the function. You are not directly accessing an index in a function.

I would call this "chaining" an indexer to the return value.

Community
  • 1
  • 1
Oded
  • 489,969
  • 99
  • 883
  • 1,009
2

Array dereferencing

Eton B.
  • 6,121
  • 5
  • 31
  • 43
1

I use the term Lookup.

ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
1

Eh.. Dangerous...??

If there are no elements in the returned array.

It's a bad habit to get into when it comes to dealing with arrays, javascript aside as per comment.

Other than that the idea is known as 'method chaining'

Adrian Regan
  • 2,240
  • 13
  • 11
  • 2
    Ok, but just to be clear this would not be dangerous in a language like JavaScript. Unless of course you try and invoke the `undefined` return value as a function. – ChaosPandion Jul 29 '10 at 11:19
1

I believe it doesn't have any special name. You're just accessing the first element of an array returned. In many languages there's nothing special in a function that returns an array.

In your case, you're not applying [] (indexing) operator to a function call. Instead, you apply it to the returned array. With precedence, it looks like this.

var firstElement = ( getSomeValues() )[0];

So there's nothing special in it.


However, there's another pattern of getting the first element, related to functional programming. For example:

($first_value, @rest_of_list) = getSomeValues();  # Perl
let first::rest = getSomeValues () in ...         (* OCaml *)

This saves the first value of a list (technically, not of an array) into $first_value, and the rest are stored in the array variable. That array variable may be omitted, and then you get the first value only.

($first_value) = getSomeValues();        # Perl
let first::_ = getSomeValues () in ...   (* OCaml *)

The more generic concept, of which what shown above is just a special case, is pattern matching (more on pattern matching). In some functional languages (none of the shown above, but in Haskell, for example) it is different: it indeed only computes first element, and doesn't make the rest of list be evaluated.

However, there's nothing that could qualify as pattern matching at all in the procedural code you showed.

Community
  • 1
  • 1
P Shved
  • 96,026
  • 17
  • 121
  • 165