Revision 8aac809024616956185de807d5c816e835ff0127 authored by Steven G. Johnson on 14 July 2023, 12:41:09 UTC, committed by Steven G. Johnson on 16 July 2023, 20:01:13 UTC
1 parent cc9019a
handling-operating-system-variation.md
# Handling Operating System Variation
When writing cross-platform applications or libraries, it is often necessary to allow for
differences between operating systems. The variable `Sys.KERNEL` can be used to handle such
cases. There are several functions in the `Sys` module intended to make this easier, such as
`isunix`, `islinux`, `isapple`, `isbsd`, `isfreebsd`, and `iswindows`. These may be used
as follows:
```julia
if Sys.iswindows()
windows_specific_thing(a)
end
```
Note that `islinux`, `isapple`, and `isfreebsd` are mutually exclusive subsets of `isunix`.
Additionally, there is a macro `@static` which makes it possible to use these functions to
conditionally hide invalid code, as demonstrated in the following examples.
Simple blocks:
```
ccall((@static Sys.iswindows() ? :_fopen : :fopen), ...)
```
Complex blocks:
```julia
@static if Sys.islinux()
linux_specific_thing(a)
elseif Sys.isapple()
apple_specific_thing(a)
else
generic_thing(a)
end
```
When nesting conditionals, the `@static` must be repeated for each level
(parentheses optional, but recommended for readability):
```julia
@static Sys.iswindows() ? :a : (@static Sys.isapple() ? :b : :c)
```
![swh spinner](/static/img/swh-spinner.gif)
Computing file changes ...