www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11811] New: Add "eval" to phobos

https://d.puremagic.com/issues/show_bug.cgi?id=11811

           Summary: Add "eval" to phobos
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: monarchdodra gmail.com


--- Comment #0 from monarchdodra gmail.com 2013-12-24 07:52:10 PST ---
The function documented here:
http://dlang.org/function.html#interpretation
Or as suggested here:
http://forum.dlang.org/thread/wqbbequembgvchmwmhnt forum.dlang.org#post-l7n550:2415ku:241:40digitalmars.com

The idea is to simply *force* the CTFE evaluation of a variable, without
declaring an enum. This can be useful when a *variable* needs to be initialized
by a function, and said function has a certain cost, but all it's arguments are
known at compile time.

for example:
int[] inc = eval!(iota(0, 10).array());
or
string s = eval!(toRoman(1998));
or
int big = eval!(fib(20));

This could have been an alternative for the implementation of octal, for
example.

It can also be used to pass as an argument to a function. For example:
void foo(T)() pure nothrow
{
    assert(someConditon, eval!(format("error %s because of %s", T.stringof,
T.sizeof));
}
Here, as you can see, the error string is evaluated at compile, so there is no
overhead during runtime. It does not require an extra variable to store the
enum, and is useable in a pure and nothrow context.


This makes initialization both more efficient, and easier: All in a single
line, and no overhead.

I'm not sure where to put this: Either typecons or conv, I'd say. Ideally, it
would be in its own package, as this is a purely convenience function that
requires nothing.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 24 2013