Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "eval" to phobos #10023

Open
dlangBugzillaToGithub opened this issue Dec 24, 2013 · 3 comments · May be fixed by #10625
Open

Add "eval" to phobos #10023

dlangBugzillaToGithub opened this issue Dec 24, 2013 · 3 comments · May be fixed by #10625

Comments

@dlangBugzillaToGithub
Copy link

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/[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.
@dlangBugzillaToGithub
Copy link
Author

andrej.mitrovich (@AndrejMitrovic) commented on 2014-04-25T13:43:07Z

Maybe in std.functional? Anyway it sounds like a neat idea.

@dlangBugzillaToGithub
Copy link
Author

bearophile_hugs commented on 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.

@dlangBugzillaToGithub
Copy link
Author

hsteoh commented on 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.

@LightBender LightBender removed the P4 label Dec 6, 2024
0xEAB added a commit to 0xEAB/phobos that referenced this issue Jan 19, 2025
@0xEAB 0xEAB linked a pull request Jan 19, 2025 that will close this issue
@0xEAB 0xEAB linked a pull request Jan 19, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants