www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22261] New: import expression does not work in __ctfe context

https://issues.dlang.org/show_bug.cgi?id=22261

          Issue ID: 22261
           Summary: import expression does not work in __ctfe context
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: ttanjo gmail.com

It would be nice if we can use import expressions in __ctfe context as shown
below.

run.dlang.io: https://t.co/ui274K3Vqa?amp=1

```dlang
import std;

void main()
{
    enum str = load(__FILE__);
}

auto load(string path)
{
    if (__ctfe)
    {
        return import(path);
    }
    else
    {
        return readText(path);
    }
}
```

It fails to compile with the following message:
```console
 rdmd playground.d
onlineapp.d(12): Error: variable `path` cannot be read at compile time ``` --
Sep 01