CTFE and BetterC compatibility
Adam D Ruppe
destructionator at gmail.com
Wed Apr 27 15:40:49 UTC 2022
On Wednesday, 27 April 2022 at 14:21:15 UTC, Claude wrote:
> The operation requiring the D-runtime is appending the array,
> but it should **only** be done at compile-time.
In that case, you want to prove to the compiler it is only called
at compile time by encapsulating the function inside a template
or defining it immediately where it is called.
Delete the stand-alone function `parse` and instead call it like
this:
```
// define it....
enum Data parsedData = function Data (string str) pure
{
Data data;
while (str.length != 0)
{
// Skip spaces
while (str[0] == ' ')
str = str[1 .. $];
// Parse single digit integer
data.digits ~= parseDigit(str[0]);
// Consume digit
str = str[1 .. $];
}
return data;
} ("5 4 2 6 9"); // and call it in the same place
```
Or if you want it to be independently defined still, you can
define it inside a helper template but you'd have to return a
basic type instead of Data, so I think calling it immediately is
what you want to do here.
There was going to be a ctfe-only thing you could put in the
function so the compiler doesn't try to generate the runtime
version, but this got killed due to internal D politics. A pity.
More information about the Digitalmars-d-learn
mailing list