HH\Map::at

Returns the value at the specified key in the current Map

Description

public function at(
  Tk $k,
): Tv

If the key is not present, an exception is thrown. If you don't want an exception to be thrown, use get() instead.

$v = $map->at($k) is equivalent to $v = $map[$k].

Parameters

  • $k - the key from which to retrieve the value.

Return Values

  • Tv - The value at the specified key; or an exception if the key does not exist.

Examples

This example prints the values at the keys red and green in the Map:

<?hh

namespace Hack\UserDocumentation\API\Examples\Map\At\ExistingKey;

$m = Map {
  'red' => '#ff0000',
  'green' => '#00ff00',
  'blue' => '#0000ff',
  'yellow' => '#ffff00',
};

// Print the value at the key 'red'
var_dump($m->at('red'));

// Print the value at the key 'yellow'
var_dump($m->at('yellow'));
Output
string(7) "#ff0000"
string(7) "#ffff00"

This example throws an OutOfBoundsException because the Map has no key 'blurple':

<?hh

namespace Hack\UserDocumentation\API\Examples\Map\At\MissingKey;

$m = Map {
  'red' => '#ff0000',
  'green' => '#00ff00',
  'blue' => '#0000ff',
  'yellow' => '#ffff00',
};

// Key 'blurple' doesn't exist (this will throw an exception)
var_dump($m->at('blurple'));
Output
Fatal error: Uncaught exception 'OutOfBoundsException' with message 'String key "blurple" is not defined' in /user-documentation/api-examples/class.Map/at/002-missing-key.php:13
Stack trace:
#0 /user-documentation/api-examples/class.Map/at/002-missing-key.php(13): HH\Map->at()
#1 {main}