Compat Package for Julia
The Compat package is designed to ease interoperability between
older and newer versions of the Julia
language. In particular, in cases where it is
impossible to write code that works with both the latest Julia
master
branch and older Julia versions, or impossible to write code
that doesn't generate a deprecation warning in some Julia version, the
Compat package provides a macro that lets you use the latest syntax
in a backwards-compatible way.
This is primarily intended for use by other Julia packages, where it is important to maintain cross-version compatibility.
Usage
To use Compat in your Julia package, add a line Compat
to the
REQUIRE
file in your package directory. Then, in your package,
shortly after the module
statement include lines like these:
using Compat
import Compat.String
and then as needed add
@compat ...Julia master syntax...
wherever you want to use syntax that differs in the latest Julia
master
(the development version of Julia).
Supported syntax
Currently, the @compat
macro supports the following syntaxes:
@compat foo.:bar
-foo.(:bar)
in 0.4 (#15032).@compat f.(args...)
-broadcast(f, args...)
in 0.4 (#15032).@compat (a::B{T}){T}(c) = d
- the Julia 0.5-style call overload.@compat Dict(foo => bar, baz => qux)
- type-inferredDict
construction. (Also works forDataStructures.OrderedDict
)@compat Dict{Foo,Bar}(foo => bar, baz => qux)
- type-declaredDict
construction. (Also works forDataStructures.OrderedDict
)@compat split(str, splitter; keywords...)
- the Julia 0.4-style keyword-basedsplit
function@compat rsplit(str, splitter; keywords...)
- the Julia 0.4-style keyword-basedrsplit
function@compat Float64(x)
,@compat UInt8(x)
, - the Julia 0.4-style numeric types constructor.@compat Tuple{foo, bar}
- Julia 0.4-style tuple types.@compat chol(A, Val{:U})
- Julia 0.4 type-stable cholesky factorizations (will not be type-stable on 0.3)@compat f(t::Timer)
- mimic the Julia 0.4 Timer class@compat Vector{Int}()
,@compat Vector{UInt8}(n)
,@compat Array{Float32}(2,2)
- Julia 0.4-style array constructors.@compat Void
-Nothing
on 0.3 (Ptr{Void}
is not changed).@compat Union{args...}
-Union(args...)
on 0.3. #11432@compat withenv(f, "a" => a, "b" => b...)
on 0.3.
Type Aliases
-
String
has undergone multiple changes: in julia 0.3 it was an abstract type and then got renamed toAbstractString
; later,ASCIIString
andUTF8String
got merged into a concrete type,String
.For packages that still need
ASCIIString
orUTF8String
on julia 0.4 and want to avoid the deprecation warning on julia 0.5, useCompat.ASCIIString
andCompat.UTF8String
instead. Note thatCompat.ASCIIString
does not guaranteeisascii
on julia 0.5. Useisascii
to check if the string is pure ASCII if needed. bytestring
has been replaced in all cases with additionalString
construction methods; for 0.3 compatibility, the usage involves replacingbytestring(args...)
with@compat String(args...)
typealias AbstractString String
-String
has been renamed toAbstractString
#8872typealias AbstractFloat FloatingPoint
-FloatingPoint
has been renamed toAbstractFloat
#12162typealias AssertionError ErrorException
-AssertionError
was introduced in #9734; before@assert
threw anErrorException
For all unsigned integer types to their equivalents with uppercase
I
. #8907Cstring
andCwstring
forPtr{UInt8}
andPtr{Cwchar_t}
, respectively: these should be used for passing NUL-terminated strings toccall
. (In Julia 0.4, using these types also checks whether the string has embedded NUL characters #10994.)typealias Irrational MathConst
-MathConst
has been renamed toIrrational
#11922typealias UDPSocket UdpSocket
-UdpSocket
has been renamed toUDPSocket
#8175typealias Base64EncodePipe Base64Pipe
-Base64Pipe
has been renamed toBase64EncodePipe
#9157typealias OutOfMemoryError MemoryError
-MemoryError
has been renamed toOutOfMemoryError
#10503
New functions
eachindex
, as infor i in eachindex(A)
, can be used in julia 0.3. This is the recommended way to iterate over each index in anAbstractArray
. On julia 0.3eachindex
just returns1:length(A)
, but in julia 0.4 it can return a more sophisticated iterator.isdiag
, which tests whether a matrix is diagonal, can be used in julia 0.3.keytype
andvaltype
, which return key and value type of Associative type, can be used in julia 0.3.tryparse
, which is a variant onBase.parse
that returns aNullable
, can be used in julia 0.3.fma(x,y,z)
andmuladd(x,y,z)
can be used in Julia 0.3 forx*y+z
.Timer(timeout::Real, repeat::Real=0.0)
andTimer(cb::Function, timeout::Real, repeat::Real=0.0)
allow julia 0.4-style Timers to be constructed and used.__precompile__(iscompiled::Bool)
andinclude_dependency(path::AbstractString)
allow Julia 0.4 precompilation information to be provided (with no effect in earlier versions). (However, to enable precompiling in 0.4, it is better to explicitly putVERSION >= v"0.4.0-dev+6521" && __precompile__()
before yourmodule
statement, so that Julia knows to precompile before anything in your module is evaluated.)isapprox(A, B)
for arrays (JuliaLang/julia#12472), and synonyms≈
(U+2248, LaTeX\approx
) and≉
(U+2249, LaTeX\napprox
) forisapprox
and!isapprox
, respectively.withenv
can be used in julia 0.3 (see the 0.4 docs). Note that you must prepend calls towithenv
with@compat
if you'd like to use it with the=>
syntax.foreach
, similar tomap
but when the return value is not needed (#13744).walkdir
, returns an iterator that walks the directory tree of a directory. (#13707)
Renamed functions
itrunc
,iround
,iceil
,ifloor
are now accessed viatrunc(T, x)
, etc. (#9133). Truncated conversions between integer types are nown % T
(#8646).Base.Random.randmtzig_exprnd
is nowrandexp
#9144sizehint
is nowsizehint!
#9278Base.IPv4
andBase.IPv6
can now acceptString
s as constructor arguments #9346randbool()
is nowrand(Bool)
andrandbool([dims])
is nowbitrand([dims])
#9569beginswith
is nowstartswith
#9583names(::DataType)
is now renamed tofieldnames
#10332parseint
andparsefloat
are nowparse(T, ...)
#10543; along the same lineBigFloat(s::String)
is nowparse(BigFloat,s)
#10955.convert(::Ptr{T}, x)
is nowBase.unsafe_convert
#9986. Compat provides an unexportedCompat.unsafe_convert
method that is aliased toBase.convert
on Julia 0.3 andBase.unsafe_convert
on Julia 0.4.gc_enable()
is nowgc_enable(true)
andgc_disable()
is nowgc_enable(false)
#11647base64
is nowbase64encode
#9157super
is nowsupertype
#14338qr(A, pivot=b)
is nowqr(A, Val{b})
, likewise forqrfact
andqrfact!
readall
andreadbytes
are nowreadstring
andread
#14660get_bigfloat_precision
is nowprecision(BigFloat)
,set_precision
issetprecision
andwith_bigfloat_precision
is now alsosetprecision
#13232get_rounding
is nowrounding
.set_rounding
andwith_rounding
are nowsetrounding
#13232Base.tty_size
(which was not exported) is nowdisplaysize
in Julia 0.5.Compat.LinAlg.checksquare
#14601issym
is nowissymmetric
#15192istext
is nowistextmime
#15708symbol
is nowSymbol
#16154; use@compat Symbol(...)
if you need Julia 0.3 compatibility.write(::IO, ::Ptr, len)
is nowunsafe_write
#14766.
New macros
@static
has been added #16219.@inline
and@noinline
have been added. On 0.3, these are "no-ops," meaning they don't actually do anything.@functorize
(not present in any Julia version) takes a function (or operator) and turns it into a functor object if one is available in the used Julia version. E.g. something likemapreduce(Base.AbsFun(), Base.MulFun(), x)
can now be written asmapreduce(@functorize(abs), @functorize(*), x)
, andf(::Base.AbsFun())
asf(::typeof(@functorize(abs)))
, to work across different Julia versions.Func{1}
can be written assupertype(typeof(@functorize(abs)))
(and so on forFunc{2}
), which will fall back toFunction
on Julia 0.5.
Other changes
Dict(ks, vs)
is nowDict(zip(ks, vs))
#8521Libc and dynamic library-related functions have been moved to the Libc and Libdl modules #10328
zero(Ptr{T})
is nowPtr{T}(0)
#8909The unexported macro
Base.@math_const
was renamed toBase.@irrational
, accessible asCompat.@irrational
on either 0.3 or 0.4 #11922remotecall
,remotecall_fetch
,remotecall_wait
, andremote_do
have the function to be executed remotely as the first argument in Julia 0.5. LoadingCompat
defines the same methods in older versions of Julia. #13338Base.FS
is nowBase.Filesystem
#12819. Compat provides an unexportedCompat.Filesystem
module that is aliased toBase.FS
on Julia 0.3 and 0.4 andBase.Filesystem
on Julia 0.5.mktemp
andmktempdir
now have variants which take a function as their first argument for automated cleanup. #9017cov
andcor
don't allow keyword arguments anymore. Loading Compat defines compatibility methods for the new API. #13465On versions of Julia that do not contain a Base.Threads module, Compat defines a Threads module containing a no-op
@threads
macro.Base.SingleAsyncWork
is nowBase.AsyncCondition
Compat provides an unexportedCompat.AsyncCondition
type that is aliased toBase.SingleAsyncWork
on Julia 0.3 and 0.4 andBase.AsyncCondition
on Julia 0.5.repeat
now accepts anyAbstractArray
#14082:Compat.repeat
supports this new API on Julia 0.3 and 0.4, and callsBase.repeat
on 0.5.OS_NAME
is nowSys.KERNEL
. OS information available asis_apple
,is_bsd
,is_linux
,is_unix
, andis_windows
. 16219
New types
Nullable
types and their associated operations.The parametric
Val{T}
"value types" can be used in julia 0.3.
Developer tips
If you're adding additional compatibility code to this package, the bin/version.sh
script is useful for extracting the version number from a git commit SHA. For example, from the git repository of julia
, run something like this:
bash $ /path/to/Compat/bin/version.sh a378b60fe483130d0d30206deb8ba662e93944da
0.5.0-dev+2023
This prints a version number corresponding to the specified commit of the form
X.Y.Z-aaa+NNNN
, and you can then test whether Julia
is at least this version by VERSION >= v"X.Y.Z-aaa+NNNN"
.