Skip to content

Commit 7971b2c

Browse files
committed
Improve generation of typealiases (JuliaMath#233)
1 parent 56edbf2 commit 7971b2c

File tree

6 files changed

+37
-18
lines changed

6 files changed

+37
-18
lines changed

src/FixedPointNumbers.jl

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -290,17 +290,24 @@ end
290290
hasalias(::Type) = false
291291
hasalias(::Type{X}) where {T<:NotBiggerThanInt64, f, X<:FixedPoint{T,f}} = f isa Int
292292

293-
# Printing. These are used to generate type-symbols, so we need them
294-
# before we include any files.
295-
function showtype(io::IO, ::Type{X}) where {X <: FixedPoint}
293+
# `alias_symbol` is used to define type aliases, so we need this before we
294+
# include "src/fixed.jl" / "src/normed.jl".
295+
function alias_symbol(@nospecialize(X))
296+
Symbol(type_prefix(X), nbitsint(X), 'f', nbitsfrac(X))
297+
end
298+
299+
function _alias_symbol(::Type{X}) where {X <: FixedPoint}
300+
if @generated
301+
sym = string(alias_symbol(X))
302+
return :(Symbol($sym))
303+
else
304+
return alias_symbol(X)
305+
end
306+
end
307+
308+
@inline function showtype(io::IO, ::Type{X}) where {X <: FixedPoint}
296309
if hasalias(X)
297-
f = nbitsfrac(X)
298-
m = nbitsint(X)
299-
write(io, typechar(X))
300-
m > 9 && write(io, Char(m ÷ 10 + 0x30))
301-
write(io, Char(m % 10 + 0x30), 'f')
302-
f > 9 && write(io, Char(f ÷ 10 + 0x30))
303-
write(io, Char(f % 10 + 0x30))
310+
write(io, _alias_symbol(X))
304311
else
305312
print(io, X)
306313
end

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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ end
2727
# TODO: remove this
2828
hasalias(::Type{F}) where {F <: Union{Fixed{Int8,8},Fixed{Int16,16},Fixed{Int32,32},Fixed{Int64,64}}} = false
2929

30-
typechar(::Type{X}) where {X <: Fixed} = 'Q'
30+
type_prefix(::Type{F}) where {F <: Fixed{<:Signed}} = :Q
3131

3232
for T in (Int8, Int16, Int32, Int64)
33-
io = IOBuffer()
3433
for f in 0:bitwidth(T)-1
35-
sym = Symbol(String(take!(showtype(io, Fixed{T,f}))))
34+
F = Fixed{T,f}
35+
sym = alias_symbol(F)
3636
@eval begin
37-
const $sym = Fixed{$T,$f}
37+
const $sym = $F
3838
export $sym
3939
end
4040
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
@@ -599,6 +599,8 @@ end
599599
end
600600

601601
@testset "show" begin
602+
@test (@test_deprecated FixedPointNumbers.typechar(Q0f7)) === 'Q'
603+
602604
iob = IOBuffer()
603605
q0f7 = reinterpret(Q0f7, signed(0xaa))
604606
show(iob, q0f7)

test/normed.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,8 @@ end
597597
end
598598

599599
@testset "show" begin
600+
@test (@test_deprecated FixedPointNumbers.typechar(N0f8)) === 'N'
601+
600602
iob = IOBuffer()
601603
n0f8 = reinterpret(N0f8, 0xaa)
602604
show(iob, n0f8)

0 commit comments

Comments
 (0)