W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
一個(gè)形狀的文字創(chuàng)造了一個(gè)有價(jià)值的列表字段初始值設(shè)定項(xiàng)指定的字段命名的形狀。在該列表中不需要的形狀類型定義的字段說明符順序相同的字段的初始化順序。例如:
<?hh
namespace Hack\UserDocumentation\Shapes\Literals\Examples\Literals;
type Point = shape('x' => int, 'y' => int);
class C {
// All the right hand side expressions are shape literals
// Can't have a shape as a constant value
const Point ORIGIN = shape('x' => 0, 'y' => 0); // initializer rejected
public static Point $p2 = shape('x' => 0, 'y' => 5); // initializer okay
public Point $p3 = shape('x' => 0, 'y' => 5); // initializer okay
}
function createPoint(int $x = 0, int $y = 0): Point {
return shape('y' => $y, 'x' => $x); // shape literal, no compile-time constant
}
function run(): void {
var_dump(createPoint(9, 3));
var_dump(new C());
}
run();
文字形狀必須初始化形狀中的所有字段。
請(qǐng)注意,與形狀一起使用的術(shù)語字面值是一個(gè)誤稱; 字段初始化器中的表達(dá)式不需要是編譯時(shí)常量。即使所有的初始化器都是常量表達(dá)式,生成的形狀文字本身也不是,因此不能在需要這樣的表達(dá)式的上下文中使用。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: