Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support new syntax for type alias with free parameters #326

Merged
merged 1 commit into from
Feb 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}` 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`.
Expand Down
30 changes: 22 additions & 8 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,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 @@ -269,7 +268,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 @@ -349,6 +348,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]
Expand Down Expand Up @@ -424,6 +433,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
Expand Down Expand Up @@ -908,10 +922,10 @@ import Base: srand, rand, rand!

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 @@ -1167,7 +1181,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})))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why doesn't @compat SubMatrix{T} = SubArray{T,2} work here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just avoiding using @compat in this file in general.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@compat is used 20 or so times in this file. Is there a reason to avoid it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just don't ever want to worry about ordering unless it's very hard to do without @compat.

(*)(A::SubMatrix, D::Diagonal) =
scale!(similar(A, promote_op(*, eltype(A), eltype(D.diag))), A, D.diag)
(*)(D::Diagonal, A::SubMatrix) =
Expand Down
10 changes: 9 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1577,7 +1577,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 @@ -1713,4 +1713,12 @@ end
@test isbits(Primitive20418{Int})
@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()

include("to-be-deprecated.jl")