www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Compile-Time Only Function

reply Moinak Bhattacharyya <moinakb001 gmail.com> writes:
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?
Jun 23 2017
next sibling parent reply Stefan Koch <uplink.coder googlemail.com> writes:
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.
Jun 23 2017
parent reply Moinak Bhattacharyya <moinakb001 gmail.com> writes:
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?
Jun 23 2017
parent reply Moinak Bhattacharyya <moinakb001 gmail.com> writes:
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?
Jun 23 2017
parent reply Mike <none none.com> writes:
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
Jun 23 2017
parent reply Moinak Bhattacharyya <moinakb001 gmail.com> writes:
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.
Jun 23 2017
parent Mike <none none.com> writes:
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!
Jun 23 2017
prev sibling parent reply Mike <none none.com> writes:
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"); }
Jun 23 2017
parent reply Moinak Bhattacharyya <moinakb001 gmail.com> writes:
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?
Jun 23 2017
next sibling parent Stefan Koch <uplink.coder googlemail.com> writes:
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
Jun 23 2017
prev sibling parent Mike <none none.com> writes:
On Friday, 23 June 2017 at 12:29:09 UTC, Moinak Bhattacharyya 
wrote:

 Do i really need to create my own itoa function?
Yes, I believe you do. I don't think the compiler has any built-in "to_string" evaluation. Even the druntime authors had to do this (https://github.com/dlang/druntime/blob/1b374914ec4e28afb87186fe51ab429df52d3322/src/cor /internal/string.d) so they wouldn't have a dependency on Phobos. Mike
Jun 23 2017