| Thread overview | |||||
|---|---|---|---|---|---|
|
March 01, 2021 How can I tell if the give parameter can be run at compile time? | ||||
|---|---|---|---|---|
| ||||
bool g(T)(T)
{
return __traits(compiles, mixin("{ enum a = t; }"));
}
int a;
enum s = "";
// both return false but g(s) is expected to return true
pragma(msg, g(s));
pragma(msg, g(a));
| ||||
March 01, 2021 Re: How can I tell if the give parameter can be run at compile time? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jack | On Monday, 1 March 2021 at 20:05:57 UTC, Jack wrote:
> int a;
> enum s = "";
> // both return false but g(s) is expected to return true
So the value must be known at compile time without any extra context. So that `a` variable might be changed somewhere else so compile time can't read or write it.
`enum` is only allowed to be set once (and at compile time!) so it is allowed at CT too.
`static immutable` generally allows it too since it must be set at declaration then never changed again.
But almost any mutable variable is no go unless it is directly returned from a function which itself can be called at compile time.
| |||
March 01, 2021 Re: How can I tell if the give parameter can be run at compile time? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jack | On Mon, Mar 01, 2021 at 08:05:57PM +0000, Jack via Digitalmars-d-learn wrote: > bool g(T)(T) > { > return __traits(compiles, mixin("{ enum a = t; }")); > } > > > int a; > enum s = ""; > // both return false but g(s) is expected to return true > pragma(msg, g(s)); > pragma(msg, g(a)); https://wiki.dlang.org/User:Quickfur/Compile-time_vs._compile-time T -- They pretend to pay us, and we pretend to work. -- Russian saying | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply