digitalmars.D.learn - std.random question
- tired_eyes (12/12) May 03 2015 Feels pretty silly, but I can't compile this:
- Dennis Ritchie (5/17) May 03 2015 void main() {
- tired_eyes (11/35) May 03 2015 Not so simple, unfortunately.
- Dennis Ritchie (15/51) May 03 2015 I think it is a bug:
- tired_eyes (3/3) May 03 2015 Hmmm.
- biozic (24/69) May 03 2015 No. The aboc code defines a field of Mystruct calld 'id', with a
- tired_eyes (1/16) May 03 2015 This make sense, thant you for the explanation.
Feels pretty silly, but I can't compile this: import std.random; auto i = uniform(0, 10); DMD spits this: /usr/include/dmd/phobos/std/random.d(1188): Error: static variable initialized cannot be read at compile time /usr/include/dmd/phobos/std/random.d(1231): called from here: rndGen() /usr/include/dmd/phobos/std/random.d(1231): called from here: uniform(a, b, rndGen()) Perhaps I'm missing something obvious? dmd 2.067.1, openSUSE 13.2 x64
May 03 2015
On Sunday, 3 May 2015 at 08:42:57 UTC, tired_eyes wrote:Feels pretty silly, but I can't compile this: import std.random; auto i = uniform(0, 10); DMD spits this: /usr/include/dmd/phobos/std/random.d(1188): Error: static variable initialized cannot be read at compile time /usr/include/dmd/phobos/std/random.d(1231): called from here: rndGen() /usr/include/dmd/phobos/std/random.d(1231): called from here: uniform(a, b, rndGen()) Perhaps I'm missing something obvious? dmd 2.067.1, openSUSE 13.2 x64void main() { import std.random; auto i = uniform(0, 10); }
May 03 2015
On Sunday, 3 May 2015 at 08:48:52 UTC, Dennis Ritchie wrote:On Sunday, 3 May 2015 at 08:42:57 UTC, tired_eyes wrote:Not so simple, unfortunately. Actual code: import std.random; struct Mystruct { auto id = uniform(0, 10); } void main() { // wahtever } ..and no luck.Feels pretty silly, but I can't compile this: import std.random; auto i = uniform(0, 10); DMD spits this: /usr/include/dmd/phobos/std/random.d(1188): Error: static variable initialized cannot be read at compile time /usr/include/dmd/phobos/std/random.d(1231): called from here: rndGen() /usr/include/dmd/phobos/std/random.d(1231): called from here: uniform(a, b, rndGen()) Perhaps I'm missing something obvious? dmd 2.067.1, openSUSE 13.2 x64void main() { import std.random; auto i = uniform(0, 10); }
May 03 2015
On Sunday, 3 May 2015 at 09:04:07 UTC, tired_eyes wrote:On Sunday, 3 May 2015 at 08:48:52 UTC, Dennis Ritchie wrote:I think it is a bug: import std.stdio, std.random; struct Mystruct { //mixin(`auto id = uniform(0, 10);`); // Error: static variable initialized cannot be read at compile time int val; } void main() { Mystruct test; test.val = uniform(0, 10); // OK writeln(test.val); mixin(`auto n = uniform(0, 10);`); // OK }On Sunday, 3 May 2015 at 08:42:57 UTC, tired_eyes wrote:Not so simple, unfortunately. Actual code: import std.random; struct Mystruct { auto id = uniform(0, 10); } void main() { // wahtever } ..and no luck.Feels pretty silly, but I can't compile this: import std.random; auto i = uniform(0, 10); DMD spits this: /usr/include/dmd/phobos/std/random.d(1188): Error: static variable initialized cannot be read at compile time /usr/include/dmd/phobos/std/random.d(1231): called from here: rndGen() /usr/include/dmd/phobos/std/random.d(1231): called from here: uniform(a, b, rndGen()) Perhaps I'm missing something obvious? dmd 2.067.1, openSUSE 13.2 x64void main() { import std.random; auto i = uniform(0, 10); }
May 03 2015
Hmmm. hap.random from http://code.dlang.org/packages/hap behaves exactly the same.
May 03 2015
On Sunday, 3 May 2015 at 09:28:40 UTC, Dennis Ritchie wrote:On Sunday, 3 May 2015 at 09:04:07 UTC, tired_eyes wrote:No. The aboc code defines a field of Mystruct calld 'id', with a type inferred from the static initializer expression 'uniform(0, 10)'. The problem is that a static initializer is... static! So the expression must be evaluated at compile-time. The uniform generator from std.random cannot be used at compile-time, thus the error. You could do: --- import std.random; struct Mystruct { int id; static opCall() { Mystruct s; s.id = uniform(0, 10); return s; } } void main() { auto s = Mystruct(); // whatever } ---On Sunday, 3 May 2015 at 08:48:52 UTC, Dennis Ritchie wrote:I think it is a bug:On Sunday, 3 May 2015 at 08:42:57 UTC, tired_eyes wrote:Not so simple, unfortunately. Actual code: import std.random; struct Mystruct { auto id = uniform(0, 10); } void main() { // wahtever } ..and no luck.Feels pretty silly, but I can't compile this: import std.random; auto i = uniform(0, 10); DMD spits this: /usr/include/dmd/phobos/std/random.d(1188): Error: static variable initialized cannot be read at compile time /usr/include/dmd/phobos/std/random.d(1231): called from here: rndGen() /usr/include/dmd/phobos/std/random.d(1231): called from here: uniform(a, b, rndGen()) Perhaps I'm missing something obvious? dmd 2.067.1, openSUSE 13.2 x64void main() { import std.random; auto i = uniform(0, 10); }
May 03 2015
import std.random; struct Mystruct { int id; static opCall() { Mystruct s; s.id = uniform(0, 10); return s; } } void main() { auto s = Mystruct(); // whatever } ---This make sense, thant you for the explanation.
May 03 2015