January 03
On Sunday, 3 January 2021 at 04:16:20 UTC, Paul Backus wrote:
> On Sunday, 3 January 2021 at 02:41:12 UTC, Paul wrote:

> hashOf is not guaranteed to produce unique values, so I would not advise using it here.

Of course it's a hashing method but also internally used for comparison and good enough, at least in that example code. By using an enum I don't assume there will be that much members to take care of anyway. The hash is also generated at compile time.

January 03
On Sunday, 3 January 2021 at 06:05:48 UTC, frame wrote:
> The hash is also generated at compile time.

Is there an easy way for me to know when code is assessed / generated at compile time?
For example, is indexing a constant compile time array compile time or run time?
Or how about functions? The hashOf documentation does not seem to hint to being done at compile time.
January 03
On Sunday, 3 January 2021 at 13:36:57 UTC, Paul wrote:

> Is there an easy way for me to know when code is assessed / generated at compile time?
> For example, is indexing a constant compile time array compile time or run time?
> Or how about functions? The hashOf documentation does not seem to hint to being done at compile time.

It's not what you do, but the context in which you do it. If a function must be evaluated at compile time, it will be.

```
string tellme() {
    if(__ctfe) return "Compile Time";
    else return "Run time";
}

void main()
{
    writeln(tellme());
    pragma(msg, tellme());
    writeln(tellme());
}
```

Since the msg pragma is a compile-time construct, tellme *must* be evaluated at compile time. The calls to writeln are just normal function calls, therefore the calls to tellme happen at runtime.

So any context where you replace a compile-time constant with a function call, you'll have CTFE (as long as it's possible [1]). It's common to use enum to force CTFE:

enum s = tellme();

[1] https://dlang.org/spec/function.html#interpretation
Next ›   Last »
1 2