www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.internals - di files vs CTFE

reply Jean-Louis Leroy <jl leroy.nyc> writes:
Hi,

While working on my openmethods library, I came across a gap that 
may be worth filling.

I have this kind of code:

// in openmethods.d
string registerMethods() { ... }

// in code using the lib
import openmethods;
mixin(registerMethods);

When I add -H, the interface file takes precedence over the 
source file, but the body of registerMethods() is removed. I 
easily found a workaround - make the function a template (see 
here 
https://github.com/jll63/openmethods.d/commit/6a7bf93932bebc53fca36
e2280c71a0cbbf3c3b) but it's a bit of a hack. I also had to remove a couple of
ctors that was there only because of my ignorance at the time.

I tried pragma(inline) but it doesn't preserve the body. Maybe it 
should, because you cannot inline without the source. Or maybe we 
need another pragma, I couldn't find a nice name but something 
along pragma(interface) or pragma(keepBody).
Aug 04 2017
next sibling parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Friday, 4 August 2017 at 13:55:29 UTC, Jean-Louis Leroy wrote:
 Hi,

 While working on my openmethods library, I came across a gap 
 that may be worth filling.

 [...]
make the function return auto. Then you'll have the body.
Aug 04 2017
parent reply Jean-Louis Leroy <jl leroy.nyc> writes:
On Friday, 4 August 2017 at 16:33:06 UTC, Stefan Koch wrote:
 On Friday, 4 August 2017 at 13:55:29 UTC, Jean-Louis Leroy 
 wrote:
 Hi,

 While working on my openmethods library, I came across a gap 
 that may be worth filling.

 [...]
make the function return auto. Then you'll have the body.
Thanks. This is, hmmm, surprising ;-)
Aug 04 2017
parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Saturday, 5 August 2017 at 00:45:51 UTC, Jean-Louis Leroy 
wrote:
 On Friday, 4 August 2017 at 16:33:06 UTC, Stefan Koch wrote:
 On Friday, 4 August 2017 at 13:55:29 UTC, Jean-Louis Leroy 
 wrote:
 Hi,

 While working on my openmethods library, I came across a gap 
 that may be worth filling.

 [...]
make the function return auto. Then you'll have the body.
Thanks. This is, hmmm, surprising ;-)
for the auto return type to work the compiler needs to infer it. in order to infer it you need the body ;)
Aug 04 2017
parent reply Jean-Louis Leroy <jl leroy.nyc> writes:
On Saturday, 5 August 2017 at 01:05:41 UTC, Stefan Koch wrote:
 On Saturday, 5 August 2017 at 00:45:51 UTC, Jean-Louis Leroy 
 wrote:
 On Friday, 4 August 2017 at 16:33:06 UTC, Stefan Koch wrote:
 On Friday, 4 August 2017 at 13:55:29 UTC, Jean-Louis Leroy 
 wrote:
 Hi,

 While working on my openmethods library, I came across a gap 
 that may be worth filling.

 [...]
make the function return auto. Then you'll have the body.
Thanks. This is, hmmm, surprising ;-)
for the auto return type to work the compiler needs to infer it. in order to infer it you need the body ;)
I see...OTOH the compiler could resolve the return type when generating the .di file, then the body would not be needed anymore. I changed my registerMethods back from a template to an auto function but I feel that I am in undocumented territory here.
Aug 04 2017
parent Stefan Koch <uplink.coder googlemail.com> writes:
On Saturday, 5 August 2017 at 02:12:33 UTC, Jean-Louis Leroy 
wrote:
 I see...OTOH the compiler could resolve the return type when 
 generating the .di file, then the body would not be needed 
 anymore. I changed my registerMethods back from a template to 
 an auto function but I feel that I am in undocumented territory 
 here.
No, the header-generation is done before sema. Therefore it cannot reslove anything. That is not going to change.
Aug 05 2017
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2017-08-04 15:55, Jean-Louis Leroy wrote:

 When I add -H, the interface file takes precedence over the source file,
 but the body of registerMethods() is removed. I easily found a
 workaround - make the function a template (see here
 https://github.com/jll63/openmethods.d/commit/6a7bf93932bebc53fca368e2280c71a0cbbf3c3b)
 but it's a bit of a hack.
I've used that workaround as well. If you still want to go with the template, I recommend making the template argument empty: string registerMethods()(string moduleName = __MODULE__) Otherwise you'll get a new template instantiation for each call to the function where the module is different. -- /Jacob Carlborg
Aug 05 2017