-
-
Notifications
You must be signed in to change notification settings - Fork 1.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
use generics in random #16283
use generics in random #16283
Conversation
## numbers returned from this proc will always be the same. | ||
## | ||
## This proc uses the default random number generator. Thus, it is **not** | ||
## thread-safe. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about this remark? Shouldn't we keep it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is already kept
Line 264 in de29bed
## This proc uses the default random number generator. Thus, it is **not** |
rand(max: float)
already has the same comments and this generic procs reuse that commentbefore.
proc rand*[T: Ordinal or SomeFloat](max: T): T {.benign.} = | ||
## Returns a random floating point number in the range `T(0) .. max`. | ||
## | ||
## Allowed types for `T` are integers, floats, and enums without holes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this comment is redundant (and only approximately correct) and should be removed, it's already specified what T is in the generated docs:
furthermore, Ordinal
is clickable (thanks to docgen) so users who see Ordinal
for the 1st time can just click to see the definition, which is explained in 1 place, where it belongs (https://nim-lang.github.io/Nim/system.html#Ordinal) => "1 definition rule" for docs
note that the definition for https://nim-lang.github.io/Nim/system.html#Ordinal should be fixed via s/enums/enums without holes/
but that's a separate issue
proc rand*(max: float): float {.benign.} = | ||
## Returns a random floating point number in the range `0.0..max`. | ||
proc rand*[T: Ordinal or SomeFloat](max: T): T {.benign.} = | ||
## Returns a random floating point number in the range `T(0) .. max`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/random floating point number/random number/
@xflywind thanks for tackling this; but this needs more care: when defined case1:
import std/random
when defined case1a:
# Error: type mismatch: got <Rand, Foo>
type Fooa = enum k0,k1,k2
echo rand(Fooa.high)
when defined case1b:
# Error: type mismatch: got <Rand, Dollar>
type Dollar = distinct int
echo rand(int.high.Dollar)
when defined case1c: # ok
echo rand(int64.high)
when defined case1d:
# Error: unhandled exception: value out of range [RangeDefect]
echo rand(uint64.high)
when defined case1e:
# Error: type mismatch: got <Rand, char>
echo (rand(char.high),) in particular this should also be updated to be made generic: proc rand*(r: var Rand; max: Natural): int {.benign.} = and you can move the old underlying proc randImpl(r: var Rand; max: Natural): int {.benign.} = ... |
let f = rand(1.0) | ||
## f = 8.717181376738381e-07 | ||
rand(state, max) | ||
result = T(rand(state, max)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cast to avoid overflow errors
This reverts commit 71e2a9e.
* use generics in random * fix
This reverts commit 71e2a9e.
* use generics in random * fix
This reverts commit 71e2a9e.
Ref timotheecour#408