Edit or run this notebook

Handling Operating System Variation

11.1 Î¼s

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:

7.1 Î¼s
if Sys.iswindows()
    windows_specific_thing(a)
end
3.1 Î¼s

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.

5.5 Î¼s

Simple blocks:

3.9 Î¼s
ccall((@static Sys.iswindows() ? :_fopen : :fopen), ...)
2.6 Î¼s

Complex blocks:

3.9 Î¼s
@static if Sys.islinux()
    linux_specific_thing(a)
else
    generic_thing(a)
end
2.5 Î¼s

When chaining conditionals (including if/elseif/end), the @static must be repeated for each level (parentheses optional, but recommended for readability):

4.5 Î¼s
@static Sys.iswindows() ? :a : (@static Sys.isapple() ? :b : :c)
2.3 Î¼s