Tuple enumeration without integers or strings
Mike Parker
aldacron at gmail.com
Sun Jan 3 14:09:20 UTC 2021
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
More information about the Digitalmars-d-learn
mailing list