Bug 11811 – Add "eval" to phobos

Status
NEW
Severity
enhancement
Priority
P4
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-12-24T07:52:10Z
Last change time
2024-12-01T16:19:42Z
Assigned to
No Owner
Creator
monarchdodra
Moved to GitHub: phobos#10023 →

Comments

Comment #0 by monarchdodra — 2013-12-24T07:52:10Z
The function documented here: http://dlang.org/function.html#interpretation Or as suggested here: http://forum.dlang.org/thread/[email protected]#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.
Comment #1 by andrej.mitrovich — 2014-04-25T13:43:07Z
Maybe in std.functional? Anyway it sounds like a neat idea.
Comment #2 by bearophile_hugs — 2014-04-25T14:00:43Z
(In reply to monarchdodra from comment #0) > The idea is to simply *force* the CTFE evaluation of a variable, without > declaring an enum. It's a nice function that I have defined in my code, but please give it a more specific name, because "eval" has usually a more general meaning (coming from Lisp-like languages). So you can name it "ctEval" or something else.
Comment #3 by hsteoh — 2014-10-02T20:08:59Z
Yeah, I think ctEval is a better name, otherwise people could misunderstand it as runtime mixin, as it is used in other languages.
Comment #4 by robert.schadek — 2024-12-01T16:19:42Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/10023 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB