Skip to content

Commit cdee03b

Browse files
committed
Improve generation of typealiases
1 parent f57e96b commit cdee03b

File tree

6 files changed

+27
-11
lines changed

6 files changed

+27
-11
lines changed

src/FixedPointNumbers.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,13 +436,17 @@ end
436436
hasalias(::Type) = false
437437
hasalias(::Type{X}) where {T<:NotBiggerThanInt64, f, X<:FixedPoint{T,f}} = f isa Int
438438

439-
# Printing. These are used to generate type-symbols, so we need them
440-
# before we include "src/fixed.jl" / "src/normed.jl".
439+
# `alias_symbol` is used to define type aliases, so we need this before we
440+
# include "src/fixed.jl" / "src/normed.jl".
441+
function alias_symbol(@nospecialize(X))
442+
Symbol(type_prefix(X), string(nbitsint(X)), 'f', string(nbitsfrac(X)))
443+
end
444+
441445
@inline function showtype(io::IO, ::Type{X}) where {X <: FixedPoint}
442446
if hasalias(X)
443447
f = nbitsfrac(X)
444448
m = nbitsint(X)
445-
write(io, typechar(X))
449+
write(io, type_prefix(X))
446450
m > 9 && write(io, Char(m ÷ 10 + 0x30))
447451
write(io, Char(m % 10 + 0x30), 'f')
448452
f > 9 && write(io, Char(f ÷ 10 + 0x30))

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
@@ -715,6 +715,8 @@ end
715715
end
716716

717717
@testset "show" begin
718+
@test (@test_deprecated FixedPointNumbers.typechar(Q0f7)) === 'Q'
719+
718720
iob = IOBuffer()
719721
q0f7 = reinterpret(Q0f7, signed(0xaa))
720722
show(iob, q0f7)

test/normed.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,8 @@ end
687687
end
688688

689689
@testset "show" begin
690+
@test (@test_deprecated FixedPointNumbers.typechar(N0f8)) === 'N'
691+
690692
iob = IOBuffer()
691693
n0f8 = reinterpret(N0f8, 0xaa)
692694
show(iob, n0f8)

0 commit comments

Comments
 (0)