Skip to content

Add "eval" to phobos #10023

@dlangBugzillaToGithub

Description

@dlangBugzillaToGithub

monarchdodra reported this on 2013-12-24T07:52:10Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=11811

CC List

Description

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions