Skip to content

Commit 6a33e48

Browse files
committed
default_sampling(T) doesn't have to return a type
1 parent a3a9d1e commit 6a33e48

3 files changed

Lines changed: 25 additions & 19 deletions

File tree

src/RandomExtensions.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ struct Cont{T} end
2020
Base.eltype(::Type{Cont{T}}) where {T} = T
2121

2222

23+
## some helper functions, not to be overloaded, except default_sampling
24+
25+
default_sampling(::Type{X}) where {X} = error("default_sampling($X) not defined")
26+
default_sampling(::X) where {X} = default_sampling(X)
27+
28+
default_gentype(::Type{T}) where {T} = val_gentype(default_sampling(T))
29+
default_gentype(::X) where {X} = default_gentype(X)
30+
31+
val_gentype(X) = gentype(X)
32+
val_gentype(::Type{X}) where {X} = X
33+
34+
2335
## includes
2436

2537
include("distributions.jl")

src/containers.jl

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# generation of some containers filled with random values
22

33

4-
default_sampling(::Type{X}) where {X} = error("default_sampling($X) not defined")
5-
default_sampling(x::X) where {X} = default_sampling(X)
6-
74
function make_argument(param)
85
if param isa Symbol
96
param
@@ -106,9 +103,9 @@ function _rand!(rng::AbstractRNG, A::SetDict, n::Integer, sp::Sampler)
106103
A
107104
end
108105

109-
rand!( A::SetDict, X) = rand!(GLOBAL_RNG, A, X)
110-
rand!(rng::AbstractRNG, A::SetDict, X) = _rand!(rng, A, length(A), sampler(rng, X))
111-
rand!( A::SetDict, ::Type{X}=default_sampling(A)) where {X} = rand!(GLOBAL_RNG, A, X)
112-
rand!(rng::AbstractRNG, A::SetDict, ::Type{X}=default_sampling(A)) where {X} = rand!(rng, A, Sampler(rng, X))
106+
rand!( A::SetDict, X=default_sampling(A)) = rand!(GLOBAL_RNG, A, X)
107+
rand!(rng::AbstractRNG, A::SetDict, X=default_sampling(A)) = _rand!(rng, A, length(A), sampler(rng, X))
108+
rand!( A::SetDict, ::Type{X}) where {X} = rand!(GLOBAL_RNG, A, X)
109+
rand!(rng::AbstractRNG, A::SetDict, ::Type{X}) where {X} = rand!(rng, A, Sampler(rng, X))
113110

114111
@make_container(T::Type{<:SetDict}, n::Integer)

src/sampling.jl

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ end
204204
if isempty(args)
205205
TT = T === Tuple ? Tuple{} :
206206
T === NTuple ? Tuple{} :
207-
T isa UnionAll && Type{T} <: Type{NTuple{N}} where N ? T{default_sampling(Tuple)} :
207+
T isa UnionAll && Type{T} <: Type{NTuple{N}} where N ? T{default_gentype(Tuple)} :
208208
T
209209
return :(Make0{$TT}())
210210
end
@@ -234,7 +234,7 @@ make(T::Type{<:Tuple}, args...) = _make(T, args...)
234234

235235
# make(Tuple, X, n::Integer)
236236

237-
default_sampling(::Type{Tuple}) = Float64
237+
default_sampling(::Type{Tuple}) = Uniform(Float64)
238238

239239
make(::Type{Tuple}, X, n::Integer) = make(NTuple{Int(n)}, X)
240240
make(::Type{Tuple}, ::Type{X}, n::Integer) where {X} = make(NTuple{Int(n)}, X)
@@ -310,8 +310,8 @@ end
310310

311311
### sets
312312

313-
default_sampling(::Type{<:AbstractSet}) = Float64
314-
default_sampling(::Type{<:AbstractSet{T}}) where {T} = T
313+
default_sampling(::Type{<:AbstractSet}) = Uniform(Float64)
314+
default_sampling(::Type{<:AbstractSet{T}}) where {T} = Uniform(T)
315315

316316
#### Set
317317

@@ -320,7 +320,7 @@ find_type(::Type{Set{T}}, _, _) where {T} = Set{T}
320320

321321
### BitSet
322322

323-
default_sampling(::Type{BitSet}) = Int8 # almost arbitrary, may change
323+
default_sampling(::Type{BitSet}) = Uniform(Int8) # almost arbitrary, may change
324324

325325
find_type(::Type{BitSet}, _, _) = BitSet
326326

@@ -329,7 +329,7 @@ find_type(::Type{BitSet}, _, _) = BitSet
329329

330330
# again same inference bug
331331
# TODO: extend to AbstractDict ? (needs to work-around the inderence bug)
332-
default_sampling(::Type{Dict{K,V}}) where {K,V} = Pair{K,V}
332+
default_sampling(::Type{Dict{K,V}}) where {K,V} = Uniform(Pair{K,V})
333333
default_sampling(D::Type{<:Dict}) = throw(ArgumentError("under-specified scalar type for $D"))
334334

335335
find_type(D::Type{<:AbstractDict{K,V}}, _, ::Integer) where {K,V} = D
@@ -344,8 +344,8 @@ find_type(::Type{Dict}, X, ::Integer) = Dict{fieldtype(va
344344

345345
### AbstractArray
346346

347-
default_sampling(::Type{<:AbstractArray{T}}) where {T} = T
348-
default_sampling(::Type{<:AbstractArray}) = Float64
347+
default_sampling(::Type{<:AbstractArray{T}}) where {T} = Uniform(T)
348+
default_sampling(::Type{<:AbstractArray}) = Uniform(Float64)
349349

350350
make(A::Type{<:AbstractArray}, X, dims::Integer...) = make(A, X, Dims(dims))
351351
make(A::Type{<:AbstractArray}, ::Type{X}, dims::Integer...) where {X} = make(A, X, Dims(dims))
@@ -363,9 +363,6 @@ rand(rng::AbstractRNG, sp::SamplerTag{A}) where {A<:AbstractArray} =
363363

364364
#### Array
365365

366-
val_gentype(X) = gentype(X)
367-
val_gentype(::Type{X}) where {X} = X
368-
369366
# cf. inference bug https://github.com/JuliaLang/julia/issues/28762
370367
# we have to write out all combinations for getting proper inference
371368
find_type(A::Type{Array{T}}, _, ::Dims{N}) where {T, N} = Array{T, N}
@@ -376,7 +373,7 @@ find_type(A::Type{Array}, X, ::Dims{N}) where {N} = Array{val_ge
376373

377374
#### BitArray
378375

379-
default_sampling(::Type{<:BitArray}) = Bool
376+
default_sampling(::Type{<:BitArray}) = Uniform(Bool)
380377

381378
find_type(::Type{BitArray{N}}, _, ::Dims{N}) where {N} = BitArray{N}
382379
find_type(::Type{BitArray}, _, ::Dims{N}) where {N} = BitArray{N}

0 commit comments

Comments
 (0)