-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
the future of Random.gentype #31968
Comments
Feedback in #27756 have been very helpful to avoid the initially proposed
Even if 1. looks like a restriction, it makes it simpler to reason about what type you get, and it doesn't prevent accomplishing anything. The main question is: for collections, do we want to allow |
Now that I have studied the code in detail, I think that removing An objection could be that this is punning on I am fine with either solution though, but a decision should be made so that it can be documented as part of the Random API. |
Yay, I feel less pressure to not disappear under a bus now 😜 (not that I want to) |
I wonder whether I should redo #31988 once #31787 is merged, to demonstrate the simplifications in the code, or wait for a decision here. My understanding is that However, I think that custom samplers in Random is a very nice interface now and more packages should be using it (in particular, Distributions.jl), which is why I want to help with this. But I recognize that clarifying this may not end up in 1.2 and we could have to wait for 1.3 for that. |
I wil merge the doc change in #31990 when CI finishes, and you can then update #31988, or wait for a decision. But I don't think that code simplification are important here, they are quite small and negligible compared to the conceptual simplification for the user.
You are correct, that's why I insisted that you don't introduce
🙏 I have been meaning to tackle this, it's great to have you on board!
There is not a big problem waiting for 1.3 for that. As of now, the official interface is using |
@rfourquet: I am wondering what would be the best way to move forward with this minor issue. If you can think of core devs who would want to comment on this but may have missed it, please mention them. I have the feeling that relatively few people follow the Random API closely, and also that the support for #27756 was lukewarm at best and no one would object to reverting it, ie just removing |
I believe that we don't really need more support to remove |
I realised that there is a PR (#28704) wanting to add The take-away for me is that, if the API for For now, my prefered solution is to keep |
Sorry, but I don't see the advantage of the syntax introduced in that PR, and for me it is not worth special-casing the Random API for this. |
I'm not totally sold on this alternative API (from this PR), but just wanted to illustrate a possible meaning to |
FWIW, I think one should not become overenthusiastic about defining samplers for types. It should only be done where the choices are pretty standard an unambiguous, which can then be used as building blocks. I imagine most (all?) of these are already in For anything beyond generating uniform random values of some From this perspective, |
I mostly agree with what you said, but don't understand the conclusion:
Whether it's called |
Sorry, I was not clear. I meant that a separate function is not needed and we can just use |
Ok. But |
I agree about the lack of urgency, since the docs now talk about I wonder if there is a label you could apply to the issue that suggests revisiting it at some point. Maybe the 1.3 milestone, or is that too early? |
The mere fact that it's an open issue should in theory be enough to suggest revisiting at some point ;-) |
I'm not sure if
In this case, if one interprets |
It am not sure about this: it would be undefined for non-array samplers. Ideally, we would have a way to describe array shapes & eltypes, without being concrete, not unlike the information consumed by Whether this should be |
This makes sense to me as a definition for a non-array distribution since |
I propose the following regarding
This would allow for code such as: function f(X, n)
v = Vector{Random.gentype(X)}(undef, n)
for i in 1:n
v[i] = rand(X)
end
return v
end This function operates effectively whether julia> using Random, StaticArrays
julia> function f(X, n)
v = Vector{Random.gentype(X)}(undef, n)
for i in 1:n
v[i] = rand(X)
end
return v
end
f (generic function with 1 method)
julia> f(4:6, 5) # Works fine with collection `X`
5-element Vector{Int64}:
4
5
6
6
5
julia> f(Bool, 5) # Works fine with `X === Bool` because `Random.gentype(Bool) == Bool`
5-element Vector{Bool}:
1
0
1
0
1
julia> f(SVector{2,Float64}, 5) # Throws an error because `Random.gentype(SVector{2,Float64}) == Float64`
ERROR: MethodError: Cannot `convert` an object of type SVector{2, Float64} to an object of type Float64
Closest candidates are:
convert(::Type{T}, ::T) where T<:Number
@ Base number.jl:6
convert(::Type{T}, ::T) where T
@ Base Base.jl:84
convert(::Type{T}, ::Number) where T<:Number
@ Base number.jl:7
...
Stacktrace:
[...]
julia> Random.gentype(T::DataType) = T
julia> f(SVector{2,Float64}, 5) # Correct!
5-element Vector{SVector{2, Float64}}:
[0.10326006658523756, 0.14320469574471628]
[0.8780611866560915, 0.6063184375760816]
[0.6257441894822869, 0.4733629411318736]
[0.07619040358834162, 0.2326119259240491]
[0.15946024991595642, 0.2648635245489229] |
Ah, there was a similar discussion: #27756 (comment) I think |
Random.gentype
was introduced in #27756, so that custom random samplers would not have to useBase.eltype
for returning element types, but the former falls back to the latter.In the discussion of the recent docs cleanup PR #31787 it was unclear whether to make
Random.gentype
part of the API.It would be great to decide
Random.gentype(sampler)
orBase.eltype(sampler)
should be the API for queryingtypeof(rand(sampler))
, especially ifsampler
is not a collection.Random.gentype
is needed at all, or should be removed from the internal code, too.@rfourquet, please comment.
The text was updated successfully, but these errors were encountered: