I don't get it. version(unittest) can't seem to use local variable

Rikki Cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 11 22:12:51 PDT 2014


On 12/07/2014 5:08 p.m., dysmondad wrote:
> I'm new to D. I've been using C since it was a baby and C++ back when it
> was only a pre-compiler for c. So, I may just be stuck thinking in C land.
>
> The issue is I am attempting to use the version(unittest) feature.
> However the compiler pukes up the error below. I'm sure it's something
> easy enough but I've not been able to find any help in Google land. I
> don't know if I have the opOpAssign defined correctly or if there's
> something wrong with the constructor or both.
>
> dysmondad at Julep:~/src/D$ gdc -c velocity.d
> velocity.d:110: error: no identifier for declarator v
> velocity.d:110: error: semicolon expected, not '*='
> velocity.d:110: error: Declaration expected, not '*='
>
>
> class Velocity
> {
> private:
>
>      float _Y = 0.0;
>      float _X = 0.0;
>
> public:
>      this (float x, float y )
>      {
>          _Y = y;
>          _X = x;
>      }
>
>      ref Velocity opOpAssign(string op) ( in float multiplier )
>      if( op == "*" )
>      {
>          _Y *= multiplier;
>          _X *= multiplier;
>          return this;
>      }
> }
>
> version(unittest)
> {
>      Velocity v = new Velocity( 2.0f, 5.0f );
>      v *= 5.0f;  // <- line 110
>      printf( "v = %s\n", to!string(v) );
> }

try:

unittest {
       Velocity v = new Velocity( 2.0f, 5.0f );
       v *= 5.0f;  // <- line 110
       printf( "v = %s\n", to!string(v) );
}

instead. Basically version is like static if, it doesn't indicate its a 
function. Instead it changes what code gets compiled.
Where as a unittest block, is essentially just a function that gets 
called at a special time, that is only compiled as if version(unittest) 
was also used.


More information about the Digitalmars-d-learn mailing list