Skip to content

Commit 84139e8

Browse files
committed
Improve generation of typealiases (JuliaMath#233)
1 parent 3310c0c commit 84139e8

File tree

6 files changed

+47
-15
lines changed

6 files changed

+47
-15
lines changed

src/FixedPointNumbers.jl

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,30 @@ function length(r::StepRange{<:FixedPoint})
261261
return div((stop - start) + step, step)
262262
end
263263

264-
# Printing. These are used to generate type-symbols, so we need them
265-
# before we include any files.
266-
function showtype(io::IO, ::Type{X}) where {X <: FixedPoint}
267-
print(io, typechar(X))
268-
f = nbitsfrac(X)
269-
m = bitwidth(X)-f-signbits(X)
270-
print(io, m, 'f', f)
264+
hasalias(::Type) = false
265+
hasalias(::Type{X}) where {T<:NotBiggerThanInt64, f, X<:FixedPoint{T,f}} = f isa Int
266+
267+
# `alias_symbol` is used to define type aliases, so we need this before we
268+
# include "src/fixed.jl" / "src/normed.jl".
269+
function alias_symbol(@nospecialize(X))
270+
Symbol(type_prefix(X), nbitsint(X), 'f', nbitsfrac(X))
271+
end
272+
273+
function _alias_symbol(::Type{X}) where {X <: FixedPoint}
274+
if @generated
275+
sym = string(alias_symbol(X))
276+
return :(Symbol($sym))
277+
else
278+
return alias_symbol(X)
279+
end
280+
end
281+
282+
@inline function showtype(io::IO, ::Type{X}) where {X <: FixedPoint}
283+
if hasalias(X)
284+
write(io, _alias_symbol(X))
285+
else
286+
print(io, X)
287+
end
271288
io
272289
end
273290
function show(io::IO, x::FixedPoint{T,f}) where {T,f}

src/deprecations.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@ function floattype(::Type{T}) where {T <: Real}
77
""", :floattype)
88
return T
99
end
10+
11+
function typechar(::Type{X}) where {X}
12+
Base.depwarn("""
13+
`typechar` was deprecated since the prefix may not be a single character in the future.
14+
We recommend not using private functions, but if you need to, use `type_prefix` instead.
15+
""", :typechar)
16+
Char(string(type_prefix(X))[1])
17+
end

src/fixed.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@ struct Fixed{T <: Signed, f} <: FixedPoint{T, f}
2424
end
2525
end
2626

27-
typechar(::Type{X}) where {X <: Fixed} = 'Q'
27+
# TODO: remove this
28+
hasalias(::Type{F}) where {F <: Union{Fixed{Int8,8},Fixed{Int16,16},Fixed{Int32,32},Fixed{Int64,64}}} = false
29+
30+
type_prefix(::Type{F}) where {F <: Fixed{<:Signed}} = :Q
2831

2932
for T in (Int8, Int16, Int32, Int64)
30-
io = IOBuffer()
3133
for f in 0:bitwidth(T)-1
32-
sym = Symbol(String(take!(showtype(io, Fixed{T,f}))))
34+
F = Fixed{T,f}
35+
sym = alias_symbol(F)
3336
@eval begin
34-
const $sym = Fixed{$T,$f}
37+
const $sym = $F
3538
export $sym
3639
end
3740
end

src/normed.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ struct Normed{T <: Unsigned, f} <: FixedPoint{T, f}
1919
end
2020
end
2121

22-
typechar(::Type{X}) where {X <: Normed} = 'N'
22+
type_prefix(::Type{N}) where {N <: Normed{<:Unsigned}} = :N
2323

2424
for T in (UInt8, UInt16, UInt32, UInt64)
25-
io = IOBuffer()
2625
for f in 1:bitwidth(T)
27-
sym = Symbol(String(take!(showtype(io, Normed{T,f}))))
26+
N = Normed{T,f}
27+
sym = alias_symbol(N)
2828
@eval begin
29-
const $sym = Normed{$T,$f}
29+
const $sym = $N
3030
export $sym
3131
end
3232
end

test/fixed.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,8 @@ end
487487
end
488488

489489
@testset "show" begin
490+
@test (@test_deprecated FixedPointNumbers.typechar(Q0f7)) === 'Q'
491+
490492
iob = IOBuffer()
491493
q0f7 = reinterpret(Q0f7, signed(0xaa))
492494
show(iob, q0f7)

test/normed.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,8 @@ end
512512
end
513513

514514
@testset "show" begin
515+
@test (@test_deprecated FixedPointNumbers.typechar(N0f8)) === 'N'
516+
515517
iob = IOBuffer()
516518
n0f8 = reinterpret(N0f8, 0xaa)
517519
show(iob, n0f8)

0 commit comments

Comments
 (0)