Thread overview
randomUUID for runtime / how to generate UUIDs in runtime
Apr 17, 2015
Ozan Süel
Apr 17, 2015
Ali Çehreli
Apr 17, 2015
Jesse Phillips
Apr 18, 2015
Ozan Süel
April 17, 2015
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
April 17, 2015
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

April 17, 2015
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();
}
April 18, 2015
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