August 30, 2016
Am Tue, 30 Aug 2016 08:57:25 +0200
schrieb Jacob Carlborg <doob@me.com>:

> On 2016-08-29 10:39, Stefan Koch wrote:
> 
> > Thanks guys.
> >
> > I just came up with a nifty little patch that makes it possible to
> > ensure that a function is _only_ used at ctfe.
> > Or the opposite.
> >
> > static assert(__ctfe, "This function is not supposed to be called
> > outside of ctfe");
> > and static assert(!__ctfe, "This function is not supposed to be
> > called during ctfe");
> >
> > similarly you can use static if (__ctfe).
> >
> > Is it worth trying to get it into master ?
> 
> Wow, absolutely :)
> 

There are some nice use cases for this:
* Do not enforce @nogc for CTFE only functions, so you can mark a
  complete module nogc: and CTFE only functions will get ignored
* Do not emit code for CTFE only functions. This is useful for bare
  metal programming: The CTFE only function can depend on druntime
  functions not available in the bare metal runtime.

These are common problems when using string mixins: The functions
to generate the mixed in code use string manipulation which is usually
not @nogc and might require runtime functions. But the code generation
function is exclusively used in CTFE.
August 30, 2016
On Tuesday, 30 August 2016 at 08:18:47 UTC, Johannes Pfau wrote:
>
> There are some nice use cases for this:
> * Do not enforce @nogc for CTFE only functions, so you can mark a
>   complete module nogc: and CTFE only functions will get ignored
> * Do not emit code for CTFE only functions. This is useful for bare
>   metal programming: The CTFE only function can depend on druntime
>   functions not available in the bare metal runtime.
>
> These are common problems when using string mixins: The functions
> to generate the mixed in code use string manipulation which is usually
> not @nogc and might require runtime functions. But the code generation
> function is exclusively used in CTFE.

I do not see how this could affect @nogc.
But yes you can circumvent the code-generation and pulling in the druntime dependency using a static if.
August 30, 2016
On Tue, Aug 30, 2016 at 10:35 AM, Stefan Koch via Digitalmars-d-announce < digitalmars-d-announce@puremagic.com> wrote:

> [snip]
> But yes you can circumvent the code-generation and pulling in the druntime
> dependency using a static if.
>

That will be awesome!


August 30, 2016
Am Tue, 30 Aug 2016 08:35:20 +0000
schrieb Stefan Koch <uplink.coder@googlemail.com>:

> I do not see how this could affect @nogc.

@nogc is only meaningful at runtime. So this could work:

module foo;
@nogc:
[...]

string ctfeOnly(string name)
{
    static assert(!__ctfe);
    // error: cannot use operator ~ in @nogc function
    return "int " ~ name ~ ";";
}
mixin(ctfeOnly("foo"));
August 30, 2016
Am Tue, 30 Aug 2016 12:42:35 +0200
schrieb Johannes Pfau <nospam@example.com>:

> @nogc is only meaningful at runtime. So this could work:
> 
> module foo;
> @nogc:
> [...]
> 
> string ctfeOnly(string name)
> {
>     static assert(!__ctfe);

This should be
    static assert(__ctfe);
of course.
August 30, 2016
On Monday, 29 August 2016 at 08:39:56 UTC, Stefan Koch wrote:
> On Monday, 29 August 2016 at 08:05:10 UTC, Rory McGuire wrote:
>> On Mon, Aug 29, 2016 at 9:51 AM, Dominikus Dittes Scherkl via
>>>
>>> The work you are doing is just awesome!
>>> Many thanks.
>>>
>>
>> +1 your work is key for our success as a community.
>>
>> R
>
> Thanks guys.
>
> I just came up with a nifty little patch that makes it possible to ensure that a function is _only_ used at ctfe.
> Or the opposite.
>
> static assert(__ctfe, "This function is not supposed to be called outside of ctfe");
> and static assert(!__ctfe, "This function is not supposed to be called during ctfe");
>
> similarly you can use static if (__ctfe).
>
> Is it worth trying to get it into master ?

I would say maybe, but let's keep separate things separate. This is a language change. I would not include it in the same series of patch that change CTFE behavior.
August 30, 2016
On Tuesday, 30 August 2016 at 17:29:19 UTC, deadalnix wrote:
worth trying to get it into master ?
>
> I would say maybe, but let's keep separate things separate. This is a language change. I would not include it in the same series of patch that change CTFE behavior.

Yes. It would be confusing. And it is not done with the patch I mentioned.
August 30, 2016
On Monday, 29 August 2016 at 08:39:56 UTC, Stefan Koch wrote:
> I just came up with a nifty little patch that makes it possible to ensure that a function is _only_ used at ctfe.
> Or the opposite.
>
> static assert(__ctfe, "This function is not supposed to be called outside of ctfe");
> and static assert(!__ctfe, "This function is not supposed to be called during ctfe");
>
> similarly you can use static if (__ctfe).
>
> Is it worth trying to get it into master ?

Yes, please. I've often wished I could use `__ctfe` in a `static if`.

August 31, 2016
On Tuesday, 30 August 2016 at 22:01:45 UTC, tsbockman wrote:
> On Monday, 29 August 2016 at 08:39:56 UTC, Stefan Koch wrote:
>> I just came up with a nifty little patch that makes it possible to ensure that a function is _only_ used at ctfe.
>> Or the opposite.
>>
>> static assert(__ctfe, "This function is not supposed to be called outside of ctfe");
>> and static assert(!__ctfe, "This function is not supposed to be called during ctfe");
>>
>> similarly you can use static if (__ctfe).
>>
>> Is it worth trying to get it into master ?
>
> Yes, please. I've often wished I could use `__ctfe` in a `static if`.

Sorry. It I overlooked a something rather important. static __ctfe is currently not possible and it's rather expensive to make it possible.
September 01, 2016
On Wed, Aug 31, 2016 at 10:43 PM, Stefan Koch via Digitalmars-d-announce < digitalmars-d-announce@puremagic.com> wrote:

> On Tuesday, 30 August 2016 at 22:01:45 UTC, tsbockman wrote:
>
>> On Monday, 29 August 2016 at 08:39:56 UTC, Stefan Koch wrote:
>>
>>> I just came up with a nifty little patch that makes it possible to
>>> ensure that a function is _only_ used at ctfe.
>>> Or the opposite.
>>>
>>> static assert(__ctfe, "This function is not supposed to be called
>>> outside of ctfe");
>>> and static assert(!__ctfe, "This function is not supposed to be called
>>> during ctfe");
>>>
>>> similarly you can use static if (__ctfe).
>>>
>>> Is it worth trying to get it into master ?
>>>
>>
>> Yes, please. I've often wished I could use `__ctfe` in a `static if`.
>>
>
> Sorry. It I overlooked a something rather important. static __ctfe is currently not possible and it's rather expensive to make it possible.
>


Surely changing the current implementation slightly could still work if we made a library implementation like:

========
// ? module std.exception.enforce_ctfe;

void main() {
ctfefunc();
}


string ctfefunc() {
static if (assertCTFE!true) {
throw new Exception("asdf");
}
return `import std.stdio; writeln("ctfe generated this");`;
}

template assertCTFE(bool b) {
enum assertCTFE = __traits(compiles, _checkCTFE());
}
void _checkCTFE() {
import std.uuid;
enum id = new UUID("8AB3060E-2cba-4f23-b74c-b52db3bdfb46");
}


the _checkCTFE() function is just a function that does something we're not
allowed to do at CTFE, but current implementation does not respect
__traits(compiles, ....);



As far as I can tell that is a bug. Thoughts?