Skip to content

Expand @static docstring #54206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions base/osutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@
"""
@static

Partially evaluate an expression at parse time.
Partially evaluate an expression at macro expansion time.

For example, `@static Sys.iswindows() ? foo : bar` will evaluate `Sys.iswindows()` and insert
either `foo` or `bar` into the expression.
This is useful in cases where a construct would be invalid on other platforms,
such as a `ccall` to a non-existent function.
`@static if Sys.isapple() foo end` and `@static foo <&&,||> bar` are also valid syntax.
This is useful in cases where a construct would be invalid in some cases, such as a `ccall`
to a os-dependent function, or macros defined in packages that are not imported.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
to a os-dependent function, or macros defined in packages that are not imported.
to an OS-dependent function, or macros defined in packages that are not imported.


`@static` requires a conditional. The conditional can be in an `if` statement, a ternary
operator, or `&&`\`||`. The conditional is evaluated by recursively expanding macros,
lowering and executing the resulting expressions. Then, the matching branch (if any) is
returned. All the other branches of the conditional are deleted before they are
macro-expanded (and lowered or executed).

# Example

Suppose we want to parse an expression `expr` that is valid only on MacOS. We could solve
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Suppose we want to parse an expression `expr` that is valid only on MacOS. We could solve
Suppose we want to parse an expression `expr` that is valid only on macOS. We could solve

this problem using `@static` with `@static if Sys.isapple() expr end`. In case we had
`expr_apple` for MacOS and `expr_others` for the other operating systems, the solution with
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`expr_apple` for MacOS and `expr_others` for the other operating systems, the solution with
`expr_apple` for macOS and `expr_others` for the other operating systems, the solution with

`@static` would be `@static Sys.isapple() ? expr_apple : expr_others`.
"""
macro static(ex)
if isa(ex, Expr)
Expand Down