Thread overview
Problems instantiating template class
Apr 06, 2019
Mek101
Apr 06, 2019
Nicholas Wilson
Apr 06, 2019
Mek101
Apr 06, 2019
Mek101
April 06, 2019
I'm rewriting from C# a small library of mine to practice with D.
I have a class:


 class WeightedRandom(T, W = float) if(isNumeric!W)
 {
	 // Fields
	 private W[T] _pairs;
	 // The total sum of all the weights;
	 private W _probabilities;

         /// Code...
 }


And when I try to instantiate an object in a unittest


 unittest
 {
	 auto wrnd = new WeightedRandom!char();
	 wrnd.normalizeAt(120.55);
 }


Compilation fails with the following error:


 source/utils/weightedRandom.d(25,18): Error: cannot pass type char as a function argument
 source/utils/weightedRandom.d(90,18): Error: template instance `utils.weightedrandom.WeightedRandom!(char, float)` error instantiating
 /usr/bin/dmd failed with exit code 1.


Same exact error with:
   WeightedRandom!(char)();
   WeightedRandom!(string, float)();


It seems like dmd thinks I'm calling a function while I'm trying to instantiate the object.
April 06, 2019
On Saturday, 6 April 2019 at 17:30:45 UTC, Mek101 wrote:
> I'm rewriting from C# a small library of mine to practice with D.
> I have a class:
>
>
>  class WeightedRandom(T, W = float) if(isNumeric!W)
>  {
> 	 // Fields
> 	 private W[T] _pairs;
> 	 // The total sum of all the weights;
> 	 private W _probabilities;
>
>          /// Code...
>  }
>
>
> And when I try to instantiate an object in a unittest
>
>
>  unittest
>  {
> 	 auto wrnd = new WeightedRandom!char();
> 	 wrnd.normalizeAt(120.55);
>  }
>
>
> Compilation fails with the following error:
>
>
>  source/utils/weightedRandom.d(25,18): Error: cannot pass type char as a function argument
>  source/utils/weightedRandom.d(90,18): Error: template instance `utils.weightedrandom.WeightedRandom!(char, float)` error instantiating
>  /usr/bin/dmd failed with exit code 1.
>
>
> Same exact error with:
>    WeightedRandom!(char)();
>    WeightedRandom!(string, float)();
>
>
> It seems like dmd thinks I'm calling a function while I'm trying to instantiate the object.

Hmm,

import std.traits;
class WeightedRandom(T, W = float) if(isNumeric!W)
 {
	 // Fields
	 private W[T] _pairs;
	 // The total sum of all the weights;
	 private W _probabilities;

         /// Code...
 }

void main()
{
    auto wrnd = new WeightedRandom!char();
}

works for me: https://run.dlang.io/is/CjSubj
April 06, 2019
On Saturday, 6 April 2019 at 17:44:25 UTC, Nicholas Wilson wrote:
> Hmm,
>
> import std.traits;
> class WeightedRandom(T, W = float) if(isNumeric!W)
>  {
> 	 // Fields
> 	 private W[T] _pairs;
> 	 // The total sum of all the weights;
> 	 private W _probabilities;
>
>          /// Code...
>  }
>
> void main()
> {
>     auto wrnd = new WeightedRandom!char();
> }
>
> works for me: https://run.dlang.io/is/CjSubj

My bad. It seems like an invisible character sent the complier crazy. I re-wrote the unittest and now it works...
Weird...
April 06, 2019
On Saturday, 6 April 2019 at 18:08:31 UTC, Mek101 wrote:
> On Saturday, 6 April 2019 at 17:44:25 UTC, Nicholas Wilson wrote:
>> Hmm,
>>
>> import std.traits;
>> class WeightedRandom(T, W = float) if(isNumeric!W)
>>  {
>> 	 // Fields
>> 	 private W[T] _pairs;
>> 	 // The total sum of all the weights;
>> 	 private W _probabilities;
>>
>>          /// Code...
>>  }
>>
>> void main()
>> {
>>     auto wrnd = new WeightedRandom!char();
>> }
>>
>> works for me: https://run.dlang.io/is/CjSubj
>
> My bad. It seems like an invisible character sent the complier crazy. I re-wrote the unittest and now it works...
> Weird...

No, it's me being wrong again.
Compiling it with 'dub test' produces the error, even in the main() function.