Rewrite the "Conditional access" section

This commit is contained in:
Andrew Voynov 2025-06-21 19:44:37 +03:00
parent 4ce7ad142f
commit 6fe6411bd8
No known key found for this signature in database
GPG Key ID: 1BE92DD685700329

View File

@ -160,25 +160,26 @@
# Conditional access # Conditional access
You can also use this in combination with the [dictionary You can also use this in combination with the [dictionary
constructor]($dictionary) to conditionally access global definitions. This constructor]($dictionary) to conditionally access global definitions. This
can, for instance, be useful to use new or experimental functionality (like can, for instance, be useful to use new or experimental functionality when
the [`target`] function) when it is available, while falling back to an it is available, while falling back to an alternative implementation if
alternative implementation if used on an older Typst version. used on an older Typst version. In particular, this allows us to create
[polyfills](https://en.wikipedia.org/wiki/Polyfill_(programming)). It can
be as simple as creating an alias to prevent warning messages, for example,
conditionally using `pattern` in Typst version 0.12, but using `tiling` in
newer ones. Since function's [API](https://en.wikipedia.org/wiki/API)
hasn't changed, simply using the `tiling` name to refer to both will unify
function's usage for old and new versions.
```example ```example
#let target = dictionary(std).at( #let tiling = dictionary(std).at("tiling", default: none)
"target", #if tiling == none { tiling = pattern }
default: () => "paged",
)
#context if target() == "html" [ ...
HTML
] else [
Paged
]
``` ```
Make sure to use this functionality responsibly as the behavior of your code Note that creating a polyfill means that you are responsible for checking
can change with new Typst versions! that it works on older Typst versions (that support `std`). That is, if it
is intended for public use.
- name: sys - name: sys
title: System title: System