Thread overview | ||||||
---|---|---|---|---|---|---|
|
July 11, 2013 isCallableCTFE trait to test whether an expression is callable during CTFE | ||||
---|---|---|---|---|
| ||||
Attachments:
| template isCallableCTFE(alias fun){ template isCallableCTFE_aux(alias T){ enum isCallableCTFE_aux=T; } enum isCallableCTFE=__traits(compiles,isCallableCTFE_aux!(fun())); } template isCallableCTFE2(fun...){ enum isCallableCTFE2=true; } unittest{ int fun1(){ return 1; } auto fun1_N(){ import std.array; //would return Error: gc_malloc cannot be interpreted at compile time, because it has no available source code due to a bug return [1].array; } int fun2(int x){ return 1; } auto fun2_N(int x){ import std.array; //same as fun1_N return [1].array; } int a1; enum a2=0; static assert(!isCallableCTFE!(()=>a1)); static assert(isCallableCTFE!(()=>a2)); static assert(isCallableCTFE!fun1); static assert(!isCallableCTFE!fun1_N); static assert(isCallableCTFE!(()=>fun2(0))); static assert(!isCallableCTFE!(()=>fun2_N(0))); //NOTE:an alternate syntax which could be implemented would be: static assert(!isCallableCTFE!(fun2_N,0))); } |
July 11, 2013 Re: isCallableCTFE trait to test whether an expression is callable during CTFE | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timothee Cour | On Thursday, 11 July 2013 at 02:17:13 UTC, Timothee Cour wrote:
> template isCallableCTFE(alias fun){
> template isCallableCTFE_aux(alias T){
> enum isCallableCTFE_aux=T;
> }
> enum isCallableCTFE=__traits(compiles,isCallableCTFE_aux!(fun()));
> }
>
> template isCallableCTFE2(fun...){
> enum isCallableCTFE2=true;
> }
>
>
> unittest{
> int fun1(){
> return 1;
> }
> auto fun1_N(){
> import std.array;
> //would return Error: gc_malloc cannot be interpreted at compile time,
> because it has no available source code due to a bug
> return [1].array;
> }
> int fun2(int x){
> return 1;
> }
> auto fun2_N(int x){
> import std.array;
> //same as fun1_N
> return [1].array;
> }
>
> int a1;
> enum a2=0;
>
> static assert(!isCallableCTFE!(()=>a1));
> static assert(isCallableCTFE!(()=>a2));
>
> static assert(isCallableCTFE!fun1);
> static assert(!isCallableCTFE!fun1_N);
>
> static assert(isCallableCTFE!(()=>fun2(0)));
> static assert(!isCallableCTFE!(()=>fun2_N(0)));
> //NOTE:an alternate syntax which could be implemented would be: static
> assert(!isCallableCTFE!(fun2_N,0)));
> }
can we add this to std.traits?
it allows (among other things) to write unittests for CTFE, etc.
|
July 11, 2013 Re: isCallableCTFE trait to test whether an expression is callable during CTFE | ||||
---|---|---|---|---|
| ||||
Posted in reply to timotheecour | On Thursday, 11 July 2013 at 03:10:38 UTC, timotheecour wrote: > On Thursday, 11 July 2013 at 02:17:13 UTC, Timothee Cour wrote: [snip] > > can we add this to std.traits? > it allows (among other things) to write unittests for CTFE, etc. Phobos has an internal helper function for testing CTFEable. https://github.com/D-Programming-Language/phobos/blob/master/std/exception.d#L1322 Kenji Hara |
July 11, 2013 Re: isCallableCTFE trait to test whether an expression is callable during CTFE | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kenji Hara | On Thursday, 11 July 2013 at 03:29:13 UTC, Kenji Hara wrote:
> On Thursday, 11 July 2013 at 03:10:38 UTC, timotheecour wrote:
>> On Thursday, 11 July 2013 at 02:17:13 UTC, Timothee Cour wrote:
> [snip]
>>
>> can we add this to std.traits?
>> it allows (among other things) to write unittests for CTFE, etc.
>
> Phobos has an internal helper function for testing CTFEable.
> https://github.com/D-Programming-Language/phobos/blob/master/std/exception.d#L1322
>
> Kenji Hara
Mine is more flexible. The one you mention can only work in a static assert statement.
We could replace it in terms of static assert(isCallableCTFE!dg) but not vice versa.
|
Copyright © 1999-2021 by the D Language Foundation