Skip to content

Commit 39e7d9b

Browse files
authored
Improve generation of typealiases (#233)
1 parent fe2135d commit 39e7d9b

File tree

6 files changed

+36
-17
lines changed

6 files changed

+36
-17
lines changed

src/FixedPointNumbers.jl

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -460,17 +460,24 @@ end
460460
hasalias(::Type) = false
461461
hasalias(::Type{X}) where {T<:NotBiggerThanInt64, f, X<:FixedPoint{T,f}} = f isa Int
462462

463-
# Printing. These are used to generate type-symbols, so we need them
464-
# before we include "src/fixed.jl" / "src/normed.jl".
463+
# `alias_symbol` is used to define type aliases, so we need this before we
464+
# include "src/fixed.jl" / "src/normed.jl".
465+
function alias_symbol(@nospecialize(X))
466+
Symbol(type_prefix(X), nbitsint(X), 'f', nbitsfrac(X))
467+
end
468+
469+
function _alias_symbol(::Type{X}) where {X <: FixedPoint}
470+
if @generated
471+
sym = string(alias_symbol(X))
472+
return :(Symbol($sym))
473+
else
474+
return alias_symbol(X)
475+
end
476+
end
477+
465478
@inline function showtype(io::IO, ::Type{X}) where {X <: FixedPoint}
466479
if hasalias(X)
467-
f = nbitsfrac(X)
468-
m = nbitsint(X)
469-
write(io, typechar(X))
470-
m > 9 && write(io, Char(m ÷ 10 + 0x30))
471-
write(io, Char(m % 10 + 0x30), 'f')
472-
f > 9 && write(io, Char(f ÷ 10 + 0x30))
473-
write(io, Char(f % 10 + 0x30))
480+
write(io, _alias_symbol(X))
474481
else
475482
print(io, X)
476483
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
@@ -718,6 +718,8 @@ end
718718
end
719719

720720
@testset "show" begin
721+
@test (@test_deprecated FixedPointNumbers.typechar(Q0f7)) === 'Q'
722+
721723
iob = IOBuffer()
722724
q0f7 = reinterpret(Q0f7, signed(0xaa))
723725
show(iob, q0f7)

test/normed.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,8 @@ end
690690
end
691691

692692
@testset "show" begin
693+
@test (@test_deprecated FixedPointNumbers.typechar(N0f8)) === 'N'
694+
693695
iob = IOBuffer()
694696
n0f8 = reinterpret(N0f8, 0xaa)
695697
show(iob, n0f8)

0 commit comments

Comments
 (0)