Skip to content

Commit c78c880

Browse files
committed
Random: introduce gentype, instead of punning on eltype
In some cases it makes sense to define what type of value `rand(rng, x)` will produce, via the newly introduced `gentype(x)`, without having `eltype(x)` be meaningful.
1 parent f41b1ec commit c78c880

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

stdlib/Random/src/Random.jl

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ export srand,
3434

3535
abstract type AbstractRNG end
3636

37+
"""
38+
Random.gentype(x)
39+
40+
Determine the type of the elements generated by calling `rand([rng], x)`.
41+
When `x` is not a type, `gentype(x)` defaults to `eltype(x)`,
42+
and for types, `gentype(x)` defaults to `x`.
43+
44+
# Examples
45+
```jldoctest
46+
julia> gentype(1:10)
47+
Int64
48+
49+
julia> gentype(Float64)
50+
Float64
51+
```
52+
"""
53+
gentype(x) = eltype(x)
54+
gentype(::Type{X}) where {X} = X
3755

3856
### integers
3957

@@ -72,7 +90,7 @@ for UI = (:UInt10, :UInt10Raw, :UInt23, :UInt23Raw, :UInt52, :UInt52Raw,
7290
end
7391
end
7492

75-
Base.eltype(::Type{<:UniformBits{T}}) where {T} = T
93+
gentype(::UniformBits{T}) where {T} = T
7694

7795
### floats
7896

@@ -88,15 +106,15 @@ const CloseOpen12_64 = CloseOpen12{Float64}
88106
CloseOpen01(::Type{T}=Float64) where {T<:AbstractFloat} = CloseOpen01{T}()
89107
CloseOpen12(::Type{T}=Float64) where {T<:AbstractFloat} = CloseOpen12{T}()
90108

91-
Base.eltype(::Type{<:FloatInterval{T}}) where {T<:AbstractFloat} = T
109+
gentype(::FloatInterval{T}) where {T<:AbstractFloat} = T
92110

93111
const BitFloatType = Union{Type{Float16},Type{Float32},Type{Float64}}
94112

95113
### Sampler
96114

97115
abstract type Sampler{E} end
98116

99-
Base.eltype(::Type{<:Sampler{E}}) where {E} = E
117+
gentype(::Sampler{E}) where {E} = E
100118

101119
# temporarily for BaseBenchmarks
102120
RangeGenerator(x) = Sampler(GLOBAL_RNG, x)
@@ -133,7 +151,7 @@ struct SamplerTrivial{T,E} <: Sampler{E}
133151
self::T
134152
end
135153

136-
SamplerTrivial(x::T) where {T} = SamplerTrivial{T,eltype(T)}(x)
154+
SamplerTrivial(x::T) where {T} = SamplerTrivial{T,gentype(x)}(x)
137155

138156
Sampler(::AbstractRNG, x, ::Repetition) = SamplerTrivial(x)
139157

@@ -145,7 +163,7 @@ struct SamplerSimple{T,S,E} <: Sampler{E}
145163
data::S
146164
end
147165

148-
SamplerSimple(x::T, data::S) where {T,S} = SamplerSimple{T,S,eltype(T)}(x, data)
166+
SamplerSimple(x::T, data::S) where {T,S} = SamplerSimple{T,S,gentype(x)}(x, data)
149167

150168
Base.getindex(sp::SamplerSimple) = sp.self
151169

@@ -223,7 +241,7 @@ end
223241
rand(r::AbstractRNG, dims::Integer...) = rand(r, Float64, Dims(dims))
224242
rand( dims::Integer...) = rand(Float64, Dims(dims))
225243

226-
rand(r::AbstractRNG, X, dims::Dims) = rand!(r, Array{eltype(X)}(undef, dims), X)
244+
rand(r::AbstractRNG, X, dims::Dims) = rand!(r, Array{gentype(X)}(undef, dims), X)
227245
rand( X, dims::Dims) = rand(GLOBAL_RNG, X, dims)
228246

229247
rand(r::AbstractRNG, X, d::Integer, dims::Integer...) = rand(r, X, Dims((d, dims...)))
@@ -232,7 +250,7 @@ rand( X, d::Integer, dims::Integer...) = rand(X, Dims((d, dims...
232250
# rand(r, ()) would match both this method and rand(r, dims::Dims)
233251
# moreover, a call like rand(r, NotImplementedType()) would be an infinite loop
234252

235-
rand(r::AbstractRNG, ::Type{X}, dims::Dims) where {X} = rand!(r, Array{eltype(X)}(undef, dims), X)
253+
rand(r::AbstractRNG, ::Type{X}, dims::Dims) where {X} = rand!(r, Array{gentype(X)}(undef, dims), X)
236254
rand( ::Type{X}, dims::Dims) where {X} = rand(GLOBAL_RNG, X, dims)
237255

238256
rand(r::AbstractRNG, ::Type{X}, d::Integer, dims::Integer...) where {X} = rand(r, X, Dims((d, dims...)))

stdlib/Random/test/runtests.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -667,8 +667,8 @@ end
667667
end
668668
end
669669

670-
@testset "eltype for UniformBits" begin
671-
@test eltype(Random.UInt52()) == UInt64
672-
@test eltype(Random.UInt52(UInt128)) == UInt128
673-
@test eltype(Random.UInt104()) == UInt128
670+
@testset "gentype for UniformBits" begin
671+
@test Random.gentype(Random.UInt52()) == UInt64
672+
@test Random.gentype(Random.UInt52(UInt128)) == UInt128
673+
@test Random.gentype(Random.UInt104()) == UInt128
674674
end

0 commit comments

Comments
 (0)