|
| 1 | +--- |
| 2 | +docid: classes-codegen-function |
| 3 | +title: CodegenFunction |
| 4 | +layout: docs |
| 5 | +permalink: /docs/classes/CodegenFunction/ |
| 6 | +--- |
| 7 | + |
| 8 | +`CodegenFunctionBase` provides shared functionality for methods and functions; |
| 9 | +`CodegenFunction` does not add to it. The recommended way to get an instance of |
| 10 | +`CodegenFunction` is to call `->codegenFunction('function_name()')` on an instance of |
| 11 | +`IHackCodegenFactory`. |
| 12 | + |
| 13 | +Basics |
| 14 | +------ |
| 15 | + |
| 16 | + - `->setReturnType(string $type)`: set the return type of the function if |
| 17 | + generating Hack code |
| 18 | + - `->setIsAsync(bool)`: if the function should use the `async` keyword or not. This |
| 19 | + doesn't affect the return type - include `Awaitable<>` in `setReturnType()` if |
| 20 | + appropriate |
| 21 | + - `->addParameter(string $param)`: add a parameter to the signature. `$param` should |
| 22 | + include both the type and variable name - you can use |
| 23 | + `->addParameterf($format, ...)` for complicated cases |
| 24 | + - `->setBody(string $code)`: set the body (implementation) of the function. You |
| 25 | + should generally use `HackBuilder` to create the string |
| 26 | + - `->setManualBody(bool $manual = true)`: if true, make the entire function body a |
| 27 | + manual section. If called, `->setBody()` is still used for the default value when |
| 28 | + generating a new file. |
| 29 | + |
| 30 | +Attributes |
| 31 | +---------- |
| 32 | + |
| 33 | +To make a function memoized (adding the `<<__Memoize>>` attribute), call |
| 34 | +`->setIsMemoized(bool $value = true)`; for other attributes, call |
| 35 | +`->setUserAttribute(string $name, ?string $value)`. |
| 36 | + |
| 37 | +CodegenMethod |
| 38 | +------------- |
| 39 | + |
| 40 | +Methods are created with `$factory->codegenMethod('methodName')`, and have some |
| 41 | +additional features over functions: |
| 42 | + |
| 43 | + - `->setIsOverride(bool $value = true)`: add the `<<__Override>>` attribute, which |
| 44 | + requires that the method is declared in a parent class |
| 45 | + - `->setIsFinal(bool $value = true)`: mark the method as final |
| 46 | + - `->setIsAbstract(bool $value = true)`: mark the method as abstract |
| 47 | + - `->setIsStatic(bool $value = true)`: mark the method as static |
| 48 | + |
| 49 | +FIXMEs |
| 50 | +------ |
| 51 | + |
| 52 | +In some cases, you need to add an `HH_FIXME` or similar to the method declaration - |
| 53 | +for example, variadics are not support in Hack before HHVM 3.15, so a fixme is |
| 54 | +needed for all variadic functions. |
| 55 | + |
| 56 | +Call `->addHHFixMe(int $code, string $why)` to add `/* HH_FIXME[$code] $why */` to |
| 57 | +the function declaration. |
| 58 | + |
| 59 | +Metadata |
| 60 | +-------- |
| 61 | + |
| 62 | +`CodegenFunctionBase` supports `->setDocBlock(string)` and |
| 63 | +`->setGeneratedFrom(CodegenGeneratedFrom)` in the same way as `CodegenFile`. |
0 commit comments