www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Template constructor instantiation syntax remains unresolved

reply James Blachly <james.blachly gmail.com> writes:
I ran in to this bug from 2013 tonight:

https://issues.dlang.org/show_bug.cgi?id=10689

Briefly, templated (and overloaded) constructor syntax is 
supported:

     this(int MAX_UNPACK = BCF_UN_ALL, T)(T *h, bcf1_t *b)
     if(is(T == VCFHeader) || is(T == bcf_hdr_t))
     {
...
     }
     this(SS)(VCFHeader *vcfhdr, string chrom, int pos, string id, 
string _ref, string alt, float qual, SS filter, )
     if (isSomeString!SS || is(SS == string[]))
     {
...
     }
     this(int MAX_UNPACK = BCF_UN_ALL)(VCFHeader *vcfhdr, string 
line)
     {
...
     }


But there is no way (?) to instantiate these with specific 
template value parameters. (Deduction works just fine)

     auto rr = new VCFRecord!BCF_UN_STR(vw.vcfhdr, "...");

for example, returns the same error it did in 2013:

Error: template instance `VCFRecord!BCF_UN_STR` VCFRecord is not 
a template declaration, it is a class



Suggestions given in past threads include templating the class, 
but in this, and other posters' examples, the constructor is 
overloaded with several ways to construct the object.  Too, I am 
not sure how templating the class would work with template value 
parameters (vice type params).

Given that templating the constructor is well-supported 
syntactically, the suggestion to use a factory function seems 
inelegant.

Thanks for listening and suggestions.
James
Jan 26 2019
parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Sunday, 27 January 2019 at 01:22:09 UTC, James Blachly wrote:
 I ran in to this bug from 2013 tonight:

 https://issues.dlang.org/show_bug.cgi?id=10689

 Briefly, templated (and overloaded) constructor syntax is 
 supported:

     this(int MAX_UNPACK = BCF_UN_ALL, T)(T *h, bcf1_t *b)
     if(is(T == VCFHeader) || is(T == bcf_hdr_t))
     {
 ...
     }
     this(SS)(VCFHeader *vcfhdr, string chrom, int pos, string 
 id, string _ref, string alt, float qual, SS filter, )
     if (isSomeString!SS || is(SS == string[]))
     {
 ...
     }
     this(int MAX_UNPACK = BCF_UN_ALL)(VCFHeader *vcfhdr, string 
 line)
     {
 ...
     }


 But there is no way (?) to instantiate these with specific 
 template value parameters. (Deduction works just fine)

     auto rr = new VCFRecord!BCF_UN_STR(vw.vcfhdr, "...");

 for example, returns the same error it did in 2013:

 Error: template instance `VCFRecord!BCF_UN_STR` VCFRecord is 
 not a template declaration, it is a class



 Suggestions given in past threads include templating the class, 
 but in this, and other posters' examples, the constructor is 
 overloaded with several ways to construct the object.  Too, I 
 am not sure how templating the class would work with template 
 value parameters (vice type params).

 Given that templating the constructor is well-supported 
 syntactically, the suggestion to use a factory function seems 
 inelegant.

 Thanks for listening and suggestions.
 James
This works fro the struct case: struct A { string s; int n; this( string s) { this.s = s; } this( int k)( int n) { this.n = n - k; } } void main() { A a = void; a.__ctor!3(42); } Not sure about the class case but you can probably concoct something similar.
Jan 26 2019