Skip to content
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

Cannot use rand at compile time #12028

Closed
bluenote10 opened this issue Aug 24, 2019 · 2 comments
Closed

Cannot use rand at compile time #12028

bluenote10 opened this issue Aug 24, 2019 · 2 comments
Labels

Comments

@bluenote10
Copy link
Contributor

Example

import random
const x = rand(10)

Current Output

<NIM_ROOT>/lib/pure/random.nim(273, 8) Error: cannot evaluate at compile time: state

Expected Output

Compiles.

Additional Information

$ nim -v
Nim Compiler Version 0.20.99 [Linux: amd64]
Compiled at 2019-08-24
Copyright (c) 2006-2019 by Andreas Rumpf

git hash: ce7f29e8e6628df209ecebb566f9cefc14243aaf
active boot switches: -d:release
@mratsim
Copy link
Collaborator

mratsim commented Aug 24, 2019

What is your use-case?

You can use rand at compile-time by passing the state:

import random
var rng {.compileTime.} = initRand(0x1337DEADBEEF)
let foo {.compileTime.} = rng.rand(10)

I use this pattern in many of my libraries.

@mratsim mratsim added Standard Library VM see also `const` label labels Aug 24, 2019
@bluenote10
Copy link
Contributor Author

Just expected it to work as is, but yes that's a sensible work-around.

To clarify, using const is also possible with this pattern:

var rng {.compileTime.} = initRand(0x1337DEADBEEF)
const foo = rng.rand(10)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants