Skip to content

Commit 7860212

Browse files
authored
Expand @static docstring (#54206)
1 parent 53f452a commit 7860212

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

base/osutils.jl

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,23 @@
33
"""
44
@static
55
6-
Partially evaluate an expression at parse time.
6+
Partially evaluate an expression at macro expansion time.
77
8-
For example, `@static Sys.iswindows() ? foo : bar` will evaluate `Sys.iswindows()` and insert
9-
either `foo` or `bar` into the expression.
10-
This is useful in cases where a construct would be invalid on other platforms,
11-
such as a `ccall` to a non-existent function.
12-
`@static if Sys.isapple() foo end` and `@static foo <&&,||> bar` are also valid syntax.
8+
This is useful in cases where a construct would be invalid in some cases, such as a `ccall`
9+
to a os-dependent function, or macros defined in packages that are not imported.
10+
11+
`@static` requires a conditional. The conditional can be in an `if` statement, a ternary
12+
operator, or `&&`\`||`. The conditional is evaluated by recursively expanding macros,
13+
lowering and executing the resulting expressions. Then, the matching branch (if any) is
14+
returned. All the other branches of the conditional are deleted before they are
15+
macro-expanded (and lowered or executed).
16+
17+
# Example
18+
19+
Suppose we want to parse an expression `expr` that is valid only on MacOS. We could solve
20+
this problem using `@static` with `@static if Sys.isapple() expr end`. In case we had
21+
`expr_apple` for MacOS and `expr_others` for the other operating systems, the solution with
22+
`@static` would be `@static Sys.isapple() ? expr_apple : expr_others`.
1323
"""
1424
macro static(ex)
1525
if isa(ex, Expr)

0 commit comments

Comments
 (0)