Skip to content

Commit 7e966c0

Browse files
authored
maketype(T, ...) defaults to T (#7)
1 parent 8cdc98a commit 7e966c0

4 files changed

Lines changed: 20 additions & 7 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,17 @@ Note that it's easy to get the result of the second interpretation via either `r
221221
`rand(String, (3,))` or `rand(String, Vector, 3)`.
222222

223223
How to extend: the `make` function is meant to be extensible, and there are some helper functions
224-
which make it easy, but the internals are not fully settled. By default, `make(T, args...)` will
224+
which make it easy, but this is still experimental. By default, `make(T, args...)` will
225225
create a `Make{maketype(T, args...)}` object, say `m`, which contain `args...` as fields. For type
226226
stable code, the `rand` machinery likes to know the exact type of the object which will be generated by
227227
`rand(m)`, and `maketype(T, args...)` is supposed to return that type. For example,
228228
`maketype(Pair, 1:3, UInt) == Pair{Int,UInt}`.
229229
Then just define `rand` for `m` like documented in the `Random` module, e.g.
230230
`rand(rng::AbstractRNG, sp::SamplerTrivial{<:Make{P}}) where {P<:Pair} = P(rand(sp[].x), rand(sp[].y))`.
231+
For convenience, `maketype(T, ...)` defaults to `T`, which means that for simple cases, only the
232+
`rand` function has to be defined. But in cases like for `Pair` above, if `maketype` is not
233+
defined, the generated type will be assumed to be `Pair`, which is not a concrete type
234+
(and hence suboptimal).
231235

232236
This package started out of frustration with the limitations of the `Random` module. Besides
233237
generating simple scalars and arrays, very little is supported out of the box. For example,

src/distributions.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Make( ::Type{T}) where {T} = Make0{T}()
1515
Make0(::Type{T}) where {T} = Make0{T}()
1616
make(::Type{T}) where {T} = Make0{maketype(T)}()
1717

18+
# default
19+
maketype(::Type{T}) where {T} = T
20+
1821
struct Make1{T,X} <: Make{T}
1922
x::X
2023
end
@@ -29,6 +32,9 @@ Make1{T}(::Type{X}) where {T,X} = Make1{T,Type{X}}(X)
2932
make(::Type{T}, x::X) where {T,X} = Make{maketype(T,x)}(x)
3033
make(::Type{T}, ::Type{X}) where {T,X} = Make{maketype(T,X)}(X)
3134

35+
# default
36+
maketype(::Type{T}, x) where {T} = T
37+
3238
find_deduced_type(::Type{T}, ::X, ) where {T,X} = deduce_type(T, gentype(X))
3339
find_deduced_type(::Type{T}, ::Type{X}) where {T,X} = deduce_type(T, X)
3440

@@ -53,6 +59,9 @@ make(::Type{T}, ::Type{X}, y::Y) where {T,X,Y} = Make{maketype(T,X,y)}(X, y
5359
make(::Type{T}, x::X, ::Type{Y}) where {T,X,Y} = Make{maketype(T,x,Y)}(x, Y)
5460
make(::Type{T}, ::Type{X}, ::Type{Y}) where {T,X,Y} = Make{maketype(T,X,Y)}(X, Y)
5561

62+
# default
63+
maketype(::Type{T}, x, y) where {T} = T
64+
5665
find_deduced_type(::Type{T}, ::X, ::Y) where {T,X,Y} = deduce_type(T, gentype(X), gentype(Y))
5766
find_deduced_type(::Type{T}, ::Type{X}, ::Y) where {T,X,Y} = deduce_type(T, X, gentype(Y))
5867
find_deduced_type(::Type{T}, ::X, ::Type{Y}) where {T,X,Y} = deduce_type(T, gentype(X), Y)
@@ -93,6 +102,8 @@ make(::Type{T}, ::Type{X}, y::Y, ::Type{Z}) where {T,X,Y,Z} = Make3{maketyp
93102
make(::Type{T}, x::X, ::Type{Y}, ::Type{Z}) where {T,X,Y,Z} = Make3{maketype(T, x, Y, Z)}(x, Y, Z)
94103
make(::Type{T}, ::Type{X}, ::Type{Y}, ::Type{Z}) where {T,X,Y,Z} = Make3{maketype(T, X, Y, Z)}(X, Y, Z)
95104

105+
# default
106+
maketype(::Type{T}, x, y, z) where {T} = T
96107

97108
# deduce_type
98109

src/sampling.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ make() = make(Float64)
1717

1818
### type
1919

20-
maketype(::Type{X}) where{X} = X
21-
2220
Sampler(RNG::Type{<:AbstractRNG}, ::Make0{X}, n::Repetition) where {X} =
2321
Sampler(RNG, X, n)
2422

test/runtests.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,10 @@ end
491491
@test s isa Arr{dim}
492492
@test length(s) == 6
493493
end
494-
@test_throws MethodError make(Matrix, 2)
495-
@test_throws MethodError make(Vector, 2, 3)
496-
@test_throws MethodError make(BitMatrix, 2)
497-
@test_throws MethodError make(BitVector, 2, 3)
494+
@test_throws MethodError rand(make(Matrix, 2))
495+
@test_throws MethodError rand(make(Vector, 2, 3))
496+
@test_throws MethodError rand(make(BitMatrix, 2))
497+
@test_throws MethodError rand(make(BitVector, 2, 3))
498498

499499
@test rand(make(Array, spString, 9)) isa Array{String}
500500
@test rand(make(BitArray, Sampler(MersenneTwister, [0, 0, 0, 1]), 9)) isa BitArray

0 commit comments

Comments
 (0)