Last active
August 29, 2015 13:57
-
-
Save BenGriffiths/9711091 to your computer and use it in GitHub Desktop.
Hack - Generics Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?hh | |
class Box<T> | |
{ | |
protected T $data; | |
public function __construct(T $data) | |
{ | |
$this->data = $data; | |
} | |
public function getData(): T | |
{ | |
return $this->data; | |
} | |
} | |
function main_gen() | |
{ | |
$gi = new Box(3); | |
$gs = new Box("Hi"); | |
$ga = new Box(array()); | |
echo $gi->getData()."\n"; | |
echo $gs->getData()."\n"; | |
echo $ga->getData()."\n"; | |
} | |
main_gen(); | |
// Output: | |
3 | |
Hi | |
Array |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment