Skip to content

Commit

Permalink
Support new syntax for type alias with free parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyichao committed Feb 12, 2017
1 parent b425216 commit 278feef
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ Currently, the `@compat` macro supports the following syntaxes:
to declare abstract and primitive types. [#20418]
This only works when `@compat` is applied directly on the declaration.

* `@compat A{T} = B{T}` to declare type alias with free parameters. [#20500]
Use `const A = B` for type alias without free parameters.

## Type Aliases

* In 0.5, `ASCIIString` and `ByteString` were deprecated, and `UTF8String` was renamed to the (now concrete) type `String`.
Expand Down
26 changes: 14 additions & 12 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ if VERSION < v"0.4.0-dev+1624"
end

if VERSION < v"0.4.0-dev+1387"
typealias AbstractString Base.String
const AbstractString = Base.String
export AbstractString
end

if VERSION < v"0.4.0-dev+3324"
typealias AssertionError ErrorException
const AssertionError = ErrorException
export AssertionError
end

Expand Down Expand Up @@ -440,16 +440,15 @@ end
istopsymbol(ex, mod, sym) = ex in (sym, Expr(:(.), mod, Expr(:quote, sym)))

if VERSION < v"0.5.0-dev+4002"
typealias Array0D{T} Array{T,0}
@inline broadcast_getindex(arg, idx) = arg[(idx - 1) % length(arg) + 1]
# Optimize for single element
@inline broadcast_getindex(arg::Number, idx) = arg
@inline broadcast_getindex(arg::Array0D, idx) = arg[1]
@inline broadcast_getindex{T}(arg::Array{T,0}, idx) = arg[1]

# If we know from syntax level that we don't need wrapping
@inline broadcast_getindex_naive(arg, idx) = arg[idx]
@inline broadcast_getindex_naive(arg::Number, idx) = arg
@inline broadcast_getindex_naive(arg::Array0D, idx) = arg[1]
@inline broadcast_getindex_naive{T}(arg::Array{T,0}, idx) = arg[1]

# For vararg support
@inline getindex_vararg(idx) = ()
Expand Down Expand Up @@ -496,7 +495,7 @@ if VERSION < v"0.5.0-dev+4002"

@inline need_full_getindex(shp) = false
@inline need_full_getindex(shp, arg1::Number) = false
@inline need_full_getindex(shp, arg1::Array0D) = false
@inline need_full_getindex{T}(shp, arg1::Array{T,0}) = false
@inline need_full_getindex(shp, arg1) = shp != size(arg1)
@inline need_full_getindex(shp, arg1, arg2) =
need_full_getindex(shp, arg1) || need_full_getindex(shp, arg2)
Expand Down Expand Up @@ -708,6 +707,9 @@ function _compat(ex::Expr)
# f.(arg) -> broadcast(f, arg)
return rewrite_broadcast(_compat(ex.args[1]), [_compat(ex.args[2])])
end
elseif (VERSION < v"0.6.0-dev.2782" && ex.head == :(=) && length(ex.args) == 2 &&
isexpr(ex.args[1], :curly))
ex.head = :typealias
elseif ex.head === :import
if VERSION < v"0.5.0-dev+4340" && length(ex.args) == 2 && ex.args[1] === :Base && ex.args[2] === :show
return quote
Expand Down Expand Up @@ -935,7 +937,7 @@ if VERSION < v"0.4.0-dev+5322"
end

if VERSION < v"0.4.0-dev+5688"
typealias Irrational MathConst
const Irrational = MathConst
@eval const $(symbol("@irrational")) = getfield(Base, symbol("@math_const"))
export Irrational
else
Expand Down Expand Up @@ -989,7 +991,7 @@ if VERSION < v"0.4.0-dev+6506"
end

if VERSION < v"0.4.0-dev+6425"
typealias AbstractFloat FloatingPoint
const AbstractFloat = FloatingPoint
export AbstractFloat
end

Expand Down Expand Up @@ -1552,10 +1554,10 @@ end

if isdefined(Core, :String) && isdefined(Core, :AbstractString)
# Not exported in order to not break code on 0.5
typealias UTF8String Core.String
typealias ASCIIString Core.String
const UTF8String = Core.String
const ASCIIString = Core.String
else
typealias String Base.ByteString
const String = Base.ByteString
if VERSION >= v"0.4.0-dev+5243"
@compat (::Type{Base.ByteString})(io::Base.AbstractIOBuffer) = bytestring(io)
elseif VERSION >= v"0.4.0-dev+1246"
Expand Down Expand Up @@ -1815,7 +1817,7 @@ end
if VERSION < v"0.5.0-dev+3669"
using Base: promote_op
import Base: *
typealias SubMatrix{T} SubArray{T,2}
eval(Expr(:typealias, :(SubMatrix{T}), :(SubArray{T,2})))
(*)(A::SubMatrix, D::Diagonal) =
scale!(similar(A, promote_op(*, eltype(A), eltype(D.diag))), A, D.diag)
(*)(D::Diagonal, A::SubMatrix) =
Expand Down
9 changes: 7 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1634,7 +1634,7 @@ end
@compat abstract type AbstractFoo20006 end
immutable ConcreteFoo20006{T<:Int} <: AbstractFoo20006 end
immutable ConcreteFoo20006N{T<:Int,N} <: AbstractFoo20006 end
typealias ConcreteFoo200061{T<:Int} ConcreteFoo20006N{T,1}
@compat ConcreteFoo200061{T<:Int} = ConcreteFoo20006N{T,1}
@test Compat.TypeUtils.isabstract(AbstractFoo20006)
@test !Compat.TypeUtils.isabstract(ConcreteFoo20006)
@test !Compat.TypeUtils.isabstract(ConcreteFoo20006N)
Expand Down Expand Up @@ -1768,4 +1768,9 @@ end
@compat primitive type Primitive20418{T} <: Ref{T} 16 end
@test !Compat.TypeUtils.isabstract(Primitive20418)
@test isbits(Primitive20418{Int})
@test sizeof(Primitive20418{Int}) == 2
@test sizeof(Primitive20418{Int}) == 2

# PR #20500
@compat A20500{T<:Integer} = Array{T,20500}
f20500() = A20500
@inferred f20500()

0 comments on commit 278feef

Please sign in to comment.