counting characters

Lodovico Giaretta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 19 05:39:56 PDT 2016


On Tuesday, 19 July 2016 at 12:23:11 UTC, celavek wrote:
> On Tuesday, 19 July 2016 at 09:57:27 UTC, Lodovico Giaretta 
> wrote:
>> On Tuesday, 19 July 2016 at 09:42:40 UTC, celavek wrote:
>>
>> Works for me:
>>
>> size_t[char] counts;
>> const string dna_chain = 
>> "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC";
>> counts['A'] = countchars(dna_chain, "A");
>
> It was failing for me as I was using "countchars!". I thought 
> that I should use the "!" in order to instantiate a template in 
> D. I'm still confused why it is working without the "!". Anyway 
> the compiler message was not very helpful.

The declaration is:

     countchars(S, S1)(S str, S1 pattern)

So the most verbose way to instantiate it is:

     countchars!(string, string)(dna_chain, "A")

But as S and S1 are the types of the two arguments, the compiler 
can easily infer them, so you can write:

     countchars(dna_chain, "A")

And have the compiler infer:

     countchars!(typeof(dna_chain), typeof("A"))(dna_chain, "A")

The error message is technically correct: you cannot instantiate 
countchars with template parameters `dna_chain` and `"A"`, which 
is what you were actually doing.


More information about the Digitalmars-d-learn mailing list