[Issue 11811] New: Add "eval" to phobos
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Tue Dec 24 07:52:14 PST 2013
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 at puremagic.com
ReportedBy: monarchdodra at gmail.com
--- Comment #0 from monarchdodra at 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: -------
More information about the Digitalmars-d-bugs
mailing list