hack形状功能

2018-11-01 17:53 更新

内部的、abstract final class Shapes提供了一些静态方法,可以对任何类型的形状进行操作。这些方法描述如下:

idx()

public static function idx(S $ shape,arraykey $ index):?Tv; 
public static function idx(S $ shape,arraykey $ index,Tv $ default):Tv;

此方法搜索名为的字段的形状$shape(其类型在此指定为S$index。如果该字段存在,则返回其值; 否则返回默认值。对于类型T的字段,该函数返回类型?T的值。$default可以提供默认值; 但是,如果省略该参数,null则使用该值。$index必须是单引号的字符串或类常量的类型stringint

<?hh

namespace Hack\UserDocumentation\Shapes\Functions\Examples\fIdx;

class C {
  const string KEYX = 'x';
  const string KEYY = 'y';
  const int KEYINTX = 10;
  const int KEYINTY = 23;
  const int KEYINTZ = 50;
}

function run(): void {
  echo "======== Shapes::idx ========\n";

  $s = shape('x' => 10, 'y' => 20);

  $v = Shapes::idx($s, 'x');    // field exists, return 10
  echo "\$v = " . (($v == null)? "null" : $v) ."\n";

  $v = Shapes::idx($s, 'y');    // field exists, return 20
  echo "\$v = " . (($v == null)? "null" : $v) ."\n";

  // field does not exist; return implict default, null
  $v = Shapes::idx($s, 'z');
  echo "\$v = " . (($v == null)? "null" : $v) ."\n";

  // field does not exist; return explicit default, -99
  $v = Shapes::idx($s, 'z', -99);
  echo "\$v = " . (($v == null)? "null" : $v) ."\n";

  echo "----------------------------\n";

  $s = shape(C::KEYINTX => 10, C::KEYINTY => 20);

  $v = Shapes::idx($s, C::KEYINTX);   // field exists, return 10
  echo "\$v = " . (($v == null)? "null" : $v) ."\n";

  $v = Shapes::idx($s, C::KEYINTY); // field exists, return 20
  echo "\$v = " . (($v == null)? "null" : $v) ."\n";

  // field does not exist; return implict default, null
  $v = Shapes::idx($s, C::KEYINTZ);
  echo "\$v = " . (($v == null)? "null" : $v) ."\n";

  // field does not exist; return explicit default, -99
  $v = Shapes::idx($s, C::KEYINTZ, -99);
  echo "\$v = " . (($v == null)? "null" : $v) ."\n";
}

run();

Output

======== Shapes::idx ========
$v = 10
$v = 20
$v = null
$v = -99
----------------------------
$v = 10
$v = 20
$v = null
$v = -99

keyExists()

public static function keyExists(S $shape, arraykey $index): bool; 

此方法搜索名为的字段的形状$shape(其类型在此指定为S$index。如果该字段存在,true则返回; 否则false返回。$index必须是单引号的字符串或类常量的类型stringint

<?hh

namespace Hack\UserDocumentation\Shapes\Functions\Examples\fKeyExists;

class C {
  const string KEYX = 'x';
  const string KEYY = 'y';
  const int KEYINTX = 10;
  const int KEYINTY = 23;
  const int KEYINTZ = 50;
}

function run(): void {
  echo "\n======== Shapes::keyExists ========\n\n";

  $s = shape('id' => "23456", 'url' => "www.example.com", 'count' => 23);

  $v = Shapes::keyExists($s, 'url');    // field exists, return true
  echo "keyExists(\$s, 'x') = " . $v ."\n";

  $v = Shapes::keyExists($s, 'name');   // does not exist, return false
  echo "keyExists(\$s, 'name') = " . $v ."\n";
}

run();

Output

======== Shapes::keyExists ========

keyExists($s, 'x') = 1
keyExists($s, 'name') =

removeKey()

public static function removeKey(S $ shape,arraykey $ index):void; 

给定一个形状$shape(其类型指定为此S)和一个字段名称$index,此方法将从该形状中删除指定的字段。如果指定的字段不存在,则删除请求将被忽略。$index必须是单引号的字符串或类常量的类型stringint

<?hh

namespace Hack\UserDocumentation\Shapes\Functions\Examples\fRemoveKey;

class C {
  const string KEYX = 'x';
  const string KEYY = 'y';
  const int KEYINTX = 10;
  const int KEYINTY = 23;
  const int KEYINTZ = 50;
}

function run(): void {
  echo "\n======== Shapes::removeKey ========\n\n";

  $s = shape();
  var_dump($s);
  Shapes::removeKey($s, 'name');  // no such field, so request ignored
  $a = Shapes::toArray($s);
  echo "# elements in array = " . count($a) . "\n";
  var_dump($s, $a);
  echo "----------------------------\n";

  $s = shape('x' => 10, 'y' => 20);
  var_dump($s);
  Shapes::removeKey($s, C::KEYX); // field 'x' removed
  $a = Shapes::toArray($s);
  echo "# elements in array = " . count($a) . "\n";
  var_dump($s, $a);
  echo "----------------------------\n";

  $s = shape('id' => "23456", 'url' => "www.example.com", 'count' => 23);
  var_dump($s);
  Shapes::removeKey($s, 'url');   // field 'url' removed
  $a = Shapes::toArray($s);
  echo "# elements in array = " . count($a) . "\n";
  var_dump($s, $a);
}

run();

Output

======== Shapes::removeKey ========

array(0) {
}
# elements in array = 0
array(0) {
}
array(0) {
}
----------------------------
array(2) {
  ["x"]=>
  int(10)
  ["y"]=>
  int(20)
}
# elements in array = 1
array(1) {
  ["y"]=>
  int(20)
}
array(1) {
  ["y"]=>
  int(20)
}
----------------------------
array(3) {
  ["id"]=>
  string(5) "23456"
  ["url"]=>
  string(15) "www.example.com"
  ["count"]=>
  int(23)
}
# elements in array = 2
array(2) {
  ["id"]=>
  string(5) "23456"
  ["count"]=>
  int(23)
}
array(2) {
  ["id"]=>
  string(5) "23456"
  ["count"]=>
  int(23)
}

toArray()

public static function toArray(S $shape): array<arraykey, mixed>; 

此方法返回一个类型的数组,array<arraykey, mixed>其中包含形状中每个字段的一个元素$shape。每个元素的键和值分别是相应字段的名称和值。数组中元素的顺序与字段插入形状的顺序相同。

<?hh

namespace Hack\UserDocumentation\Shapes\Functions\Examples\fToArray;

class C {
  const string KEYX = 'x';
  const string KEYY = 'y';
  const int KEYINTX = 10;
  const int KEYINTY = 23;
  const int KEYINTZ = 50;
}

function run(): void {
  echo "\n======== Shapes::toArray ========\n\n";

  $s = shape();
  $a = Shapes::toArray($s);   // returns an array of 0 elements
  echo "# elements in array = " . count($a) . "\n";
  var_dump($s, $a);
  echo "----------------------------\n";

  $s = shape('x' => 10, 'y' => 20);
  $a = Shapes::toArray($s);
  echo "# elements in array = " . count($a) . "\n";
  var_dump($s, $a);
  echo "----------------------------\n";

  $s = shape('y' => 20, 'x' => 10);
  $a = Shapes::toArray($s);
  echo "# elements in array = " . count($a) . "\n";
  var_dump($s, $a);
  echo "----------------------------\n";

  $s = shape('id' => "23456", 'url' => "www.example.com", 'count' => 23);
  // returns an array of 3 elements, of type string, string, and int, respectively
  $a = Shapes::toArray($s);
  echo "# elements in array = " . count($a) . "\n";
}

run();

Output

======== Shapes::toArray ========

# elements in array = 0
array(0) {
}
array(0) {
}
----------------------------
# elements in array = 2
array(2) {
  ["x"]=>
  int(10)
  ["y"]=>
  int(20)
}
array(2) {
  ["x"]=>
  int(10)
  ["y"]=>
  int(20)
}
----------------------------
# elements in array = 2
array(2) {
  ["y"]=>
  int(20)
  ["x"]=>
  int(10)
}
array(2) {
  ["y"]=>
  int(20)
  ["x"]=>
  int(10)
}
----------------------------
# elements in array = 3


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号