[Issue 14018] New: Treat CTFE-able functions as pure in CTFE-only contexts
    via Digitalmars-d-bugs 
    digitalmars-d-bugs at puremagic.com
       
    Tue Jan 20 12:47:02 PST 2015
    
    
  
https://issues.dlang.org/show_bug.cgi?id=14018
          Issue ID: 14018
           Summary: Treat CTFE-able functions as pure in CTFE-only
                    contexts
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: DMD
          Assignee: nobody at puremagic.com
          Reporter: verylonglogin.reg at gmail.com
If a function is executed during CTFE it's effectively `pure` for given
parameters. This enhancement proposes to treat a function as a `pure` one in
such contexts.
E.g. it will allow implicit conversion of a function result to `immutable`:
---
int g = 0;
int[] f(int i)
{
    if(i)
        ++g;
    return new int[1];
}
static immutable s1 = f(0); // OK
void main()
{
    static immutable s2 = f(0); // OK
    immutable s3 = f(0); // error, the context doesn't require CTFE
}
---
Pros:
Allow previously rejected but logically valid code to compile.
Cons:
Function return type is treated differently depending on a calling context
(CTFE or not) which complicates language type system.
--
    
    
More information about the Digitalmars-d-bugs
mailing list