www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - "extern" template instantiation

reply "Mathias LANG" <pro.mathias.lang gmail.com> writes:
Hi everyone,
This is going to be a silly question.

I have the following files:
-- heavy.d --
import std.regex;
enum r = 
ctRegex!(r"^(\w+)\s+([a-zA-Z0-9/]+)\?a=(\w+)&b=(\w+)&c=(\w+)([&\D+=\w+)]*)&y=([0-9A-Fa-z]+)&z=([0-9A-Fa-z]+)$");

-- main.d --
import heavy;
void main() {}

-- test.d --
import heavy;
void test() {}

So I had a Makefile, and hit compile. *BAM*
1m40s latter, I switched from GDC to DMD, and updated my dmd to 
bleeding edge.
I though it might help because of: 
http://d.puremagic.com/issues/show_bug.cgi?id=11284

$ dmd
DMD64 D Compiler v2.064-devel-b12d171

But it didn't (time make):
dmd -debug  -c main.d -ofobjs/debug//main.o
dmd -debug  -c heavy.d -ofobjs/debug//heavy.o
dmd -debug  -c test.d -ofobjs/debug//test.o
	 - Linking bfake
real	0m19.893s
user	0m11.092s
sys	0m2.416s


With the import commented out (and make clean'ed):

dmd -debug  -c main.d -ofobjs/debug//main.o
dmd -debug  -c heavy.d -ofobjs/debug//heavy.o
dmd -debug  -c test.d -ofobjs/debug//test.o
	 - Linking bfake
real	0m5.636s
user	0m4.124s
sys	0m0.872s

So I thought of:
public alias typeof(ctRegex!(my-super-long-string)) APIRegex;

in a .di file, but I get a linker error, and it stills need to 
instantiate the template anyway.

Can anyone point me to the right way to do this ? Thanks!
Oct 22 2013
parent reply "Dicebot" <public dicebot.lv> writes:
Using `enum` with ctRegex is discouraged because it is a dumb 
copy-paste upon every enum usage. I'd recommend to use global 
variable initialized during compile-time:

```
auto r = 
ctRegex!(r"^(\w+)\s+([a-zA-Z0-9/]+)\?a=(\w+)&b=(\w+)&c=(\w+)([&\D+=\w+)]*)&y=([0-9A-Fa-z]+)&z=([0-9A-Fa-z]+)$");
```

Though it is pretty complex ctRegex and compiling it is a good 
stress test for any compiler :)
Oct 23 2013
parent reply "Mathias LANG" <pro.mathias.lang gmail.com> writes:
On Wednesday, 23 October 2013 at 11:58:17 UTC, Dicebot wrote:
 Using `enum` with ctRegex is discouraged because it is a dumb 
 copy-paste upon every enum usage. I'd recommend to use global 
 variable initialized during compile-time:

 ```
 auto r = 
 ctRegex!(r"^(\w+)\s+([a-zA-Z0-9/]+)\?a=(\w+)&b=(\w+)&c=(\w+)([&\D+=\w+)]*)&y=([0-9A-Fa-z]+)&z=([0-9A-Fa-z]+)$");
 ```

 Though it is pretty complex ctRegex and compiling it is a good 
 stress test for any compiler :)
Using auto was my first try. Unfortunately it has the same issue (slighly faster, but still "dumb C/P". So far I didn't find any way to tell the compiler not to compile it if I'm not using it (aside from versions). As the type itself takes ages to compile, any reasonable-sized project with few regexes will not be able to use ctRegex in dev mode. I suppose there's no way to create a function that returns a generic ctRegex object without loosing the advantages ? Note: The regex might seems complex, but actually isn't. It parses an home-made REST API based on query authentification: http://broadcast.oreilly.com/2009/12/principles-for-standardized-rest authentication.html . To me it sounded like a simple use case.
Oct 24 2013
parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
24-Oct-2013 20:30, Mathias LANG пишет:
 On Wednesday, 23 October 2013 at 11:58:17 UTC, Dicebot wrote:
 Using `enum` with ctRegex is discouraged because it is a dumb
 copy-paste upon every enum usage. I'd recommend to use global variable
 initialized during compile-time:

 ```
 auto r =
 ctRegex!(r"^(\w+)\s+([a-zA-Z0-9/]+)\?a=(\w+)&b=(\w+)&c=(\w+)([&\D+=\w+)]*)&y=([0-9A-Fa-z]+)&z=([0-9A-Fa-z]+)$");

 ```

 Though it is pretty complex ctRegex and compiling it is a good stress
 test for any compiler :)
Using auto was my first try. Unfortunately it has the same issue (slighly faster, but still "dumb C/P". So far I didn't find any way to tell the compiler not to compile it if I'm not using it (aside from versions). As the type itself takes ages to compile, any reasonable-sized project with few regexes will not be able to use ctRegex in dev mode. I suppose there's no way to create a function that returns a generic ctRegex object without loosing the advantages ?
Something to the extent of auto getMeARegex() { static r = ctRegex!(r"^(\w+)\s+([a-zA-Z0-9/]+)\?a=(\w+)&b=(\w+)&c=(\w+)([&\D+=\w+)]*)&y=([0-9A-Fa-z]+)& =([0-9A-Fa-z]+)$"); return r; } _Might_ work. Failing that make it an 0-arg template: auto getMeARegex()(){ ... }
 Note: The regex might seems complex, but actually isn't.
 It parses an home-made REST API  based on query authentification:
 http://broadcast.oreilly.com/2009/12/principles-for-standardized-rest-authentication.html
 . To me it sounded like a simple use case.
-- Dmitry Olshansky
Oct 24 2013