www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - randomUUID for runtime / how to generate UUIDs in runtime

reply "Ozan =?UTF-8?B?U8O8ZWwi?= <ozan.sueel gmail.com> writes:
Hi!

In the std.uuid PHOBOS library doc I found:

      trusted UUID randomUUID();
     This function generates a random number based UUID from a 
random number generator.
     CTFE: This function is not supported at compile time.

Than trying a call like
     auto uuid = randomUUID()

results in an error message:
     /usr/include/dmd/phobos/std/random.d(1138,10): Error: static 
variable seeded cannot be read at compile time

My understanding: randomUUID, which is available for runtime 
requires a variable from std.random, which is available only in 
compile time.
Sounds like a mathematical riddle, or?

Why I'm asking: I want to create class instances with an unique 
id as default.
But now that great idea seems to be a death end.

Anyone has an idea how to generate UUIDs in runtime?

Thanks & regards,
Ozan
Apr 17 2015
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/17/2015 07:05 AM, "Ozan =?UTF-8?B?U8O8ZWwi?= 
<ozan.sueel gmail.com>" wrote:

      CTFE: This function is not supported at compile time.

 Than trying a call like
      auto uuid = randomUUID()

 results in an error message:
      /usr/include/dmd/phobos/std/random.d(1138,10): Error: static
 variable seeded cannot be read at compile time
I don't see the issue. Both of those errors indicate the same thing: "not available at compile time." You may have read one of them wrong. (?) There is no problem with randomUUID() at run time. However, you may be needing an expression at compile time, which in turn needs randomUUID(): import std.uuid; int foo() { auto uuid = randomUUID(); // <-- Compiles and runs fine return 42; } void main() { enum e = foo(); // <-- The actual problem } Although, the error message I received is not exactly the same as yours: Error: static variable initialized cannot be read at compile time Ali
Apr 17 2015
prev sibling parent reply "Jesse Phillips" <Jessekphillips+D gmail.com> writes:
On Friday, 17 April 2015 at 14:05:26 UTC, Ozan Süel wrote:
 Why I'm asking: I want to create class instances with an unique 
 id as default.
 But now that great idea seems to be a death end.
Sounds like your code is something like: class foo { auto myid = randomUUID(); } The compiler requires that myid be initialized with a compile time value in this instance, which can not be obtained by randomUUID(). You'll need to use a constructor: this() { myid = randomUUID(); }
Apr 17 2015
parent "Ozan =?UTF-8?B?U8O8ZWwi?= <ozan.sueel gmail.com> writes:
On Friday, 17 April 2015 at 19:17:32 UTC, Jesse Phillips wrote:
 On Friday, 17 April 2015 at 14:05:26 UTC, Ozan Süel wrote:
...
 You'll need to use a constructor:
 this() {
     myid = randomUUID();
 }
That's it. Thanks Ali & Jesse. I have to put it in a constructor. Regards, Ozan
Apr 18 2015