From 7442bbd3be3315565dafc7964d2f62a329803a50 Mon Sep 17 00:00:00 2001 From: Yichao Yu Date: Sun, 12 Feb 2017 14:44:44 -0500 Subject: [PATCH] Support new syntax for type alias with free parameters. --- README.md | 3 +++ src/Compat.jl | 38 ++++++++++++++++++++++++++------------ test/runtests.jl | 12 ++++++++++-- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 7e079610a..57542aa74 100644 --- a/README.md +++ b/README.md @@ -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}` or `@compat const 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`. diff --git a/src/Compat.jl b/src/Compat.jl index bc63c3165..41ff5a9a4 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -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 @@ -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) = () @@ -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) @@ -576,6 +575,16 @@ if VERSION < v"0.5.0-dev+4002" end end +if VERSION < v"0.6.0-dev.2782" + function new_style_typealias(ex::ANY) + isexpr(ex, :(=)) || return false + ex = ex::Expr + return length(ex.args) == 2 && isexpr(ex.args[1], :curly) + end +else + new_style_typealias(ex) = false +end + function _compat(ex::Expr) if ex.head === :call f = ex.args[1] @@ -708,6 +717,11 @@ function _compat(ex::Expr) # f.(arg) -> broadcast(f, arg) return rewrite_broadcast(_compat(ex.args[1]), [_compat(ex.args[2])]) end + elseif new_style_typealias(ex) + ex.head = :typealias + elseif ex.head === :const && length(ex.args) == 1 && new_style_typealias(ex.args[1]) + ex = ex.args[1]::Expr + 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 @@ -935,7 +949,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 @@ -989,7 +1003,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 @@ -1552,10 +1566,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" @@ -1815,7 +1829,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) = diff --git a/test/runtests.jl b/test/runtests.jl index 94260a5d9..5260e7806 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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) @@ -1768,4 +1768,12 @@ end @compat primitive type Primitive20418{T} <: Ref{T} 16 end @test !Compat.TypeUtils.isabstract(Primitive20418) @test isbits(Primitive20418{Int}) -@test sizeof(Primitive20418{Int}) == 2 \ No newline at end of file +@test sizeof(Primitive20418{Int}) == 2 + +# PR #20500 +@compat A20500{T<:Integer} = Array{T,20500} +@compat const A20500_2{T<:Union{Int,Float32}} = Pair{T,T} +f20500() = A20500 +f20500_2() = A20500_2 +@inferred f20500() +@inferred f20500_2()