February 03, 2016 Re: Why this code can't take advantage from CTFE? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | On 02/03/2016 11:39 PM, Andrea Fontana wrote:
> On Wednesday, 3 February 2016 at 17:49:39 UTC, Marc Schütz wrote:
>> On Wednesday, 3 February 2016 at 16:07:59 UTC, Messenger wrote:
>>> What is a good way to try to force it? Using enum? Then optionally
>>> copying the value once to avoid the "manifest constant" copy/paste
>>> behaviour, where applicable?
>>
>> template forceCTFE(alias expr) {
>> alias forceCTFE = expr;
>> }
>>
>> auto value = forceCTFE!(some_expression());
>
> Interesting :)
> Thank you!
I would use enum forceCTFE(alias expr)=expr; though. With alias it won't force compile-time evaluation of expressions that can be interpreted as symbols.
|
February 05, 2016 Re: Why this code can't take advantage from CTFE? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On Wednesday, 3 February 2016 at 22:45:47 UTC, Timon Gehr wrote: > I would use enum forceCTFE(alias expr)=expr; though. With alias it won't force compile-time evaluation of expressions that can be interpreted as symbols. I've a code that build a JSON object using a wrapper over std.json. This code: ---- enum forceCTFE(alias expr)=expr; auto j = forceCTFE!( JSOB ( "hello", JSOB("world", 3), "arr", JSAB("hello", JSOB("world", 1)) ) ); j.put("/hello/world", "!"); j.put("/hello/blah", 42); // Here j == {"arr":["hello",{"world":1}],"hello":{"blah":42,"world":"!"}} ---- shows a 10% performance gain over this: ---- auto j = JSOB ( "hello", JSOB("world", 3), "arr", JSAB("hello", JSOB("world", 1)) ); j.put("/hello/world", "!"); j.put("/hello/blah", 42); ---- (inside a loop). And it's just a small json in this case. That's pretty good IMHO :) |
February 05, 2016 Re: Why this code can't take advantage from CTFE? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | On Friday, 5 February 2016 at 09:36:37 UTC, Andrea Fontana wrote:
> On Wednesday, 3 February 2016 at 22:45:47 UTC, Timon Gehr wrote:
>> I would use enum forceCTFE(alias expr)=expr; though. With alias it won't force compile-time evaluation of expressions that can be interpreted as symbols.
>
> I've a code that build a JSON object using a wrapper over std.json.
>
> This code:
>
> ----
>
> enum forceCTFE(alias expr)=expr;
>
> auto j = forceCTFE!(
> JSOB
> (
> "hello", JSOB("world", 3),
> "arr", JSAB("hello", JSOB("world", 1))
> )
> );
>
> j.put("/hello/world", "!");
> j.put("/hello/blah", 42);
> // Here j == {"arr":["hello",{"world":1}],"hello":{"blah":42,"world":"!"}}
Reading/parsing a JSON or a XML using std.json / std.xml could be done on CTFE ?
|
February 05, 2016 Re: Why this code can't take advantage from CTFE? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Luis | On Friday, 5 February 2016 at 09:49:38 UTC, Luis wrote:
> Reading/parsing a JSON or a XML using std.json / std.xml could be done on CTFE ?
parseJSON() from std.json doesn't work with CTFE.
But I can build objects with with my code that works over std.json.
So if you convert (with mixins)
{ "hello" : "world" }
to
JSOB("hello", "world)
You can parse it ar compile time. :)
|
Copyright © 1999-2021 by the D Language Foundation