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.
To use Compat in your Julia package, add a line Compat
to the
REQUIRE
file in your package directory. Then, in your package,
after a using Compat
statement to load Compat, simply use:
@compat ...Julia master syntax...
wherever you want to use syntax that differs in the latest Julia
master
(the development version of Julia).
Currently, the @compat
macro supports the following syntaxes:
-
@compat Dict(foo => bar, baz => qux)
- type-inferredDict
construction. -
@compat Dict{Foo,Bar}(foo => bar, baz => qux)
- type-declaredDict
construction. -
@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.
-
typealias AbstractString String
-String
has been renamed toAbstractString
#8872 -
For all unsigned integer types to their equivalents with uppercase
I
. #8907
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.
-
itrunc
,iround
,iceil
,ifloor
are now accessed viatrunc(T, x)
, etc. #9133 -
Base.Random.randmtzig_exprnd
is nowrandexp
#9144 -
sizehint
is nowsizehint!
#9278 -
Base.IPv4
andBase.IPv6
can now acceptString
s as constructor arguments #9346 -
randbool()
is nowrand(Bool)
andrandbool([dims])
is nowbitrand([dims])
#9569 -
beginswith
is nowstartswith
#9583 -
|>
,>>
,.>
, and.>>
are nowpipe
#10211 -
names(::DataType)
is now renamed tofieldnames
#10332 -
parseint
andparsefloat
are nowparse(T, ...)
#10543 -
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.
@inline
and@noinline
have been added. On 0.3, these are "no-ops," meaning they don't actually do anything.
-
Dict(ks, vs)
is nowDict(zip(ks, vs))
#8521 -
Libc and dynamic library-related functions have been moved to the Libc and Libdl modules #10328
-
zero(Ptr{T})
is nowPtr{T}(0)
#8909
Nullable
types and their associated operations.
If you're adding additional compatibility code to this package, the following shell script is useful for extracting the version number from a git commit SHA:
#! /bin/bash
last_tag=$(git describe --tags --abbrev=0)
git rev-list $1 ^$last_tag | wc -l | sed -e 's/[^[:digit:]]//g'
This will print a number XXXX
, and you can then test whether Julia
is at least this version by VERSION >= v"0.4.0-dev+XXXX"
(assuming
it is a commit from the 0.4 development cycle).