Skip to content

Commit b072867

Browse files
committed
update according to feedback, with option 2)
1 parent c3f46d2 commit b072867

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

stdlib/Random/src/Random.jl

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,24 @@ export srand,
3535
abstract type AbstractRNG end
3636

3737
"""
38-
Random.gentype(x)
38+
Random.gentype(T)
3939
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`.
40+
Determine the type of the elements generated by calling `rand([rng], x)`,
41+
where `x::T`, and `x` is not a type.
42+
The definition `gentype(x) = gentype(typeof(x))` is provided for convenience,
43+
and `gentype(T)` defaults to `eltype(T)`.
44+
NOTE: `rand([rng], X)`, where `X` is a type, is always assumed to produce
45+
an object of type `X`.
4346
4447
# Examples
4548
```jldoctest
4649
julia> gentype(1:10)
4750
Int64
48-
49-
julia> gentype(Float64)
50-
Float64
5151
```
5252
"""
53-
gentype(x) = eltype(x)
54-
gentype(::Type{X}) where {X} = X
53+
gentype(::Type{X}) where {X} = eltype(X)
54+
gentype(x) = gentype(typeof(x))
55+
5556

5657
### integers
5758

@@ -90,7 +91,7 @@ for UI = (:UInt10, :UInt10Raw, :UInt23, :UInt23Raw, :UInt52, :UInt52Raw,
9091
end
9192
end
9293

93-
gentype(::UniformBits{T}) where {T} = T
94+
gentype(::Type{<:UniformBits{T}}) where {T} = T
9495

9596
### floats
9697

@@ -106,15 +107,15 @@ const CloseOpen12_64 = CloseOpen12{Float64}
106107
CloseOpen01(::Type{T}=Float64) where {T<:AbstractFloat} = CloseOpen01{T}()
107108
CloseOpen12(::Type{T}=Float64) where {T<:AbstractFloat} = CloseOpen12{T}()
108109

109-
gentype(::FloatInterval{T}) where {T<:AbstractFloat} = T
110+
gentype(::Type{<:FloatInterval{T}}) where {T<:AbstractFloat} = T
110111

111112
const BitFloatType = Union{Type{Float16},Type{Float32},Type{Float64}}
112113

113114
### Sampler
114115

115116
abstract type Sampler{E} end
116117

117-
gentype(::Sampler{E}) where {E} = E
118+
gentype(::Type{<:Sampler{E}}) where {E} = E
118119

119120
# temporarily for BaseBenchmarks
120121
RangeGenerator(x) = Sampler(GLOBAL_RNG, x)
@@ -151,7 +152,7 @@ struct SamplerTrivial{T,E} <: Sampler{E}
151152
self::T
152153
end
153154

154-
SamplerTrivial(x::T) where {T} = SamplerTrivial{T,gentype(x)}(x)
155+
SamplerTrivial(x::T) where {T} = SamplerTrivial{T,gentype(T)}(x)
155156

156157
Sampler(::AbstractRNG, x, ::Repetition) = SamplerTrivial(x)
157158

@@ -163,14 +164,14 @@ struct SamplerSimple{T,S,E} <: Sampler{E}
163164
data::S
164165
end
165166

166-
SamplerSimple(x::T, data::S) where {T,S} = SamplerSimple{T,S,gentype(x)}(x, data)
167+
SamplerSimple(x::T, data::S) where {T,S} = SamplerSimple{T,S,gentype(T)}(x, data)
167168

168169
Base.getindex(sp::SamplerSimple) = sp.self
169170

170171
# simple sampler carrying a (type) tag T and data
171172
struct SamplerTag{T,S,E} <: Sampler{E}
172173
data::S
173-
SamplerTag{T}(s::S) where {T,S} = new{T,S,eltype(T)}(s)
174+
SamplerTag{T}(s::S) where {T,S} = new{T,S,gentype(T)}(s)
174175
end
175176

176177

@@ -250,7 +251,7 @@ rand( X, d::Integer, dims::Integer...) = rand(X, Dims((d, dims...
250251
# rand(r, ()) would match both this method and rand(r, dims::Dims)
251252
# moreover, a call like rand(r, NotImplementedType()) would be an infinite loop
252253

253-
rand(r::AbstractRNG, ::Type{X}, dims::Dims) where {X} = rand!(r, Array{gentype(X)}(undef, dims), X)
254+
rand(r::AbstractRNG, ::Type{X}, dims::Dims) where {X} = rand!(r, Array{X}(undef, dims), X)
254255
rand( ::Type{X}, dims::Dims) where {X} = rand(GLOBAL_RNG, X, dims)
255256

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

0 commit comments

Comments
 (0)