Jump to page: 1 2
Thread overview
Compile-Time Only Function
Jun 23, 2017
Stefan Koch
Jun 23, 2017
Mike
Jun 23, 2017
Mike
Jun 23, 2017
Mike
Jun 23, 2017
Stefan Koch
Jun 23, 2017
Mike
June 23, 2017
Is there an annotation that declares a function compile-time only? I'm attempting to use a mixin string generating function that uses standard druntime imports, but I'm working in a no-stdlib environment. I never need to use this function at runtime (as is the case, I imagine, with a great many of mixin string generating functions) so is there a way I can omit it?
June 23, 2017
On Friday, 23 June 2017 at 10:46:11 UTC, Moinak Bhattacharyya wrote:
> Is there an annotation that declares a function compile-time only? I'm attempting to use a mixin string generating function that uses standard druntime imports, but I'm working in a no-stdlib environment. I never need to use this function at runtime (as is the case, I imagine, with a great many of mixin string generating functions) so is there a way I can omit it?

There is no such annotation.
However what you can do is to put your function into a function literal and enclose it with if (__ctfe)

which would look like this

string GenString(bla, blubb)
{
  if (__ctfe) {
    auto functionBody = () {
     // .... your body with imports and all
    }
    return functionBody(bla, blubb);
  }
  else
  {
    assert(0);
  }
}

this should hopefully keep the compile-time code out of your executable.
It is even more effective if you use a immediately invoked function literal directly inside the mixin.
which will also cause the symbol for the function to invisible and therefore not generated.
June 23, 2017
On Friday, 23 June 2017 at 10:52:25 UTC, Stefan Koch wrote:
> string GenString(bla, blubb)

That still keeps string (an associative array) as a runtime symbol thus requires druntime. How can I remove the string-y part?
June 23, 2017
On Friday, 23 June 2017 at 11:04:52 UTC, Moinak Bhattacharyya wrote:
> On Friday, 23 June 2017 at 10:52:25 UTC, Stefan Koch wrote:
>> string GenString(bla, blubb)
>
> That still keeps string (an associative array) as a runtime symbol thus requires druntime. How can I remove the string-y part?

My mistake, it's typeinfo that's being included now. So is memcpy. I don't know why, as they are never used at runtime. How can I avoid this?
June 23, 2017
On Friday, 23 June 2017 at 11:24:14 UTC, Moinak Bhattacharyya wrote:

> My mistake, it's typeinfo that's being included now. So is memcpy. I don't know why, as they are never used at runtime. How can I avoid this?

Unless you're using GDC with this (https://github.com/D-Programming-GDC/GDC/pull/456), TypeInfo is always going to be included.

The only way to get a build is write TypeInfo stubs like this (https://github.com/JinShil/minimal_druntime_experiment/blob/master/source/d/runtime/object.d#L51)

If you're trying to use D without the runtime, you're going to have to provide certain implementations depending on which features of D you are using.

Mike

June 23, 2017
On Friday, 23 June 2017 at 11:29:52 UTC, Mike wrote:
> On Friday, 23 June 2017 at 11:24:14 UTC, Moinak Bhattacharyya wrote:
>
>> My mistake, it's typeinfo that's being included now. So is memcpy. I don't know why, as they are never used at runtime. How can I avoid this?
>
> Unless you're using GDC with this (https://github.com/D-Programming-GDC/GDC/pull/456), TypeInfo is always going to be included.
>
> The only way to get a build is write TypeInfo stubs like this (https://github.com/JinShil/minimal_druntime_experiment/blob/master/source/d/runtime/object.d#L51)
>
> If you're trying to use D without the runtime, you're going to have to provide certain implementations depending on which features of D you are using.
>
> Mike

I think there should be a decoupling of the compile-time and runtime library. When I provide a stub object.d, the compile-time function fails due to missing implementations.
June 23, 2017
On Friday, 23 June 2017 at 11:50:08 UTC, Moinak Bhattacharyya wrote:

> I think there should be a decoupling of the compile-time and runtime library. When I provide a stub object.d, the compile-time function fails due to missing implementations.

Welcome to the club!
June 23, 2017
On Friday, 23 June 2017 at 10:46:11 UTC, Moinak Bhattacharyya wrote:
> Is there an annotation that declares a function compile-time only? I'm attempting to use a mixin string generating function that uses standard druntime imports, but I'm working in a no-stdlib environment. I never need to use this function at runtime (as is the case, I imagine, with a great many of mixin string generating functions) so is there a way I can omit it?

I don't know of any way to generate a compile-time error if a function cannot be evaluated at compile-time.  I think you will have to use a manifest constant (https://dlang.org/spec/enum.html#manifest_constants) to force evaluation at compile time.

// If you're not using druntime, you'll have to define the
// string type as an alias to an immutable char array.
// See https://github.com/dlang/druntime/blob/master/src/object.d#L41
alias immutable(char)[] string;

string GenString(string a, string b)
{
    return a ~ ", " ~ b;
}

void main()
{
    // `result` is a manifest constant that contains your
    // compile-time string, do as you wish with it
    enum result = GenString("Hello", "World");
}

June 23, 2017
On Friday, 23 June 2017 at 12:23:23 UTC, Mike wrote:
> alias immutable(char)[] string;
>
> string GenString(string a, string b)
> {
>     return a ~ ", " ~ b;
> }
>
> void main()
> {
>     // `result` is a manifest constant that contains your
>     // compile-time string, do as you wish with it
>     enum result = GenString("Hello", "World");
> }

The problem is, I have to concat a string with an int thus I need to use std.conv. This creates all sorts of problems. Do i really need to create my own itoa function?
June 23, 2017
On Friday, 23 June 2017 at 12:29:09 UTC, Moinak Bhattacharyya wrote:
> On Friday, 23 June 2017 at 12:23:23 UTC, Mike wrote:
>> alias immutable(char)[] string;
>>
>> string GenString(string a, string b)
>> {
>>     return a ~ ", " ~ b;
>> }
>>
>> void main()
>> {
>>     // `result` is a manifest constant that contains your
>>     // compile-time string, do as you wish with it
>>     enum result = GenString("Hello", "World");
>> }
>
> The problem is, I have to concat a string with an int thus I need to use std.conv. This creates all sorts of problems. Do i really need to create my own itoa function?

The the one from https://github.com/UplinkCoder/bf-ctfe/blob/master/source/bf_compiler.d
and https://github.com/UplinkCoder/bf-ctfe/blob/master/source/bf_fastmath.d
« First   ‹ Prev
1 2