Skip to content

Commit

Permalink
Update guides/hack from HHVM repository
Browse files Browse the repository at this point in the history
  • Loading branch information
dlreeves authored and github-actions[bot] committed Dec 13, 2024
1 parent 2ca5842 commit bd83afc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
38 changes: 38 additions & 0 deletions guides/hack/11-built-in-types/54-class.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
```yamlmeta
{
"fbonly messages": [
"This type is under migration, and is allowed to flow to the [classname type](/hack/built-in-types/classname) for existing string use cases. There is [an internal wiki](https://www.internalfb.com/intern/wiki/Hack_Foundation/classnameC_vs._classC/) describing the type separation."
]
}
```

Hack supports passing references to classes for use in instatiation and static
member access.

```Hack no-extract
<<__ConsistentConstruct>>
class Employee {
public static function getKind(): int { return 4; };
}
function f(class<Employee> $cls): void {
$w = new $cls(); // create an object of $cls
$i = $cls::getKind();
}
```

Function `f` can be called with a reference to the class `Employee` or any of
its subclasses using `::class` literals (`SomeClassName::class`).

```Hack no-extract
class Intern extends Employee {
// ...
}
function demo(): void {
f(Employee::class); // will call: new Employee();
f(Intern::class); // will call: new Intern();
f(Vector::class); // typechecker error!
}
```
19 changes: 14 additions & 5 deletions guides/hack/11-built-in-types/55-classname.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
For the most part, we deal with class types directly via their names. For example:
```yamlmeta
{
"fbonly messages": [
"This type is under migration, with the class-specific use cases described below shifting to the new [class type](/hack/built-in-types/class). There is [an internal wiki](https://www.internalfb.com/intern/wiki/Hack_Foundation/classnameC_vs._classC/) describing the type separation."
]
}
```

For the most part, we deal with class types directly via their names. For
example:

```Hack no-extract
class Employee {
Expand All @@ -8,7 +17,8 @@ class Employee {
$emp = new Employee();
```

However, in some applications, it is useful to be able to abstract a class' name rather than to hard-code it. Consider the following:
However, in some applications, it is useful to be able to abstract a class' name
rather than to hard-code it. Consider the following:

```Hack file:employee.hack
<<__ConsistentConstruct>>
Expand Down Expand Up @@ -37,6 +47,5 @@ function demo(): void {
```

In Hack code, the class names must be specified using "`::class` literals"
(`SomeClassName::class`). At runtime, these are regular strings (`SomeClassName::class === 'SomeClassName'`).

The value of an expression of a classname type can be converted implicitly or explicitly to `string`.
(`SomeClassName::class`). The value of an expression of a classname type can be
converted implicitly or explicitly to `string`.

0 comments on commit bd83afc

Please sign in to comment.