www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - opCmp and unittest as a nested function

reply Alexandr Druzhinin <drug2004 bk.ru> writes:
Hello!
http://dpaste.dzfl.pl/f7364d416cb2
Error appeared when I defined opCmp and now I can't understand what's 
the reason of the error. Why does tween need access to unittest?

I looked over bugzilla but I hadn't managed to find relevant issue if 
it's a bug.

Thank in advance.
Apr 09 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Alexandr Druzhinin:

 http://dpaste.dzfl.pl/f7364d416cb2
 Error appeared when I defined opCmp and now I can't understand 
 what's the reason of the error. Why does tween need access to 
 unittest?
Try to use "static struct" instead of a "struct". Bye, bearophile
Apr 09 2014
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
And you need to use opEquals:

unittest {
     static struct Point2D {
         double x, y;

         bool opEquals(const Point2D rhs) const {
             return false;
         }
     }

     auto p2d0 = Point2D(0, 0);
     auto p2d1 = Point2D(1, 0);

     assert(tween(p2d0, p2d1, 1 / 3.0) == Point2D(1 / 3.0, 0));
}


opEquals/onHash/opCmp are minefields. I don't know why the D 
compiler doesn't add a large amount of errors and warnings to 
turns this minefield in something a bit safer.

Also use std.math.feqrel to compare floating point values.

Bye,
bearophile
Apr 09 2014
parent Alexandr Druzhinin <drug2004 bk.ru> writes:
09.04.2014 13:55, bearophile пишет:
 And you need to use opEquals:

 unittest {
      static struct Point2D {
          double x, y;

          bool opEquals(const Point2D rhs) const {
              return false;
          }
      }

      auto p2d0 = Point2D(0, 0);
      auto p2d1 = Point2D(1, 0);

      assert(tween(p2d0, p2d1, 1 / 3.0) == Point2D(1 / 3.0, 0));
 }


 opEquals/onHash/opCmp are minefields. I don't know why the D compiler
 doesn't add a large amount of errors and warnings to turns this
 minefield in something a bit safer.
It would be very nice.
 Also use std.math.feqrel to compare floating point values.
Thanks!
 Bye,
 bearophile
Apr 09 2014
prev sibling parent reply Alexandr Druzhinin <drug2004 bk.ru> writes:
09.04.2014 13:45, bearophile пишет:
 Alexandr Druzhinin:

 http://dpaste.dzfl.pl/f7364d416cb2
 Error appeared when I defined opCmp and now I can't understand what's
 the reason of the error. Why does tween need access to unittest?
Try to use "static struct" instead of a "struct". Bye, bearophile
Thank you! It works. But where can I read about this issue?
Apr 09 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Alexandr Druzhinin:

 Thank you! It works. But where can I read about this issue?
Reading about this "issue" is not good. What you can read about is how unittests are implemented in D (as functions) and what's the difference between static structs and nonstatic ones when they are defined inside a function. Bye, bearophile
Apr 09 2014
parent reply Alexandr Druzhinin <drug2004 bk.ru> writes:
09.04.2014 14:13, bearophile пишет:
 Reading about this "issue" is not good. What you can read about is how
 unittests are implemented in D (as functions) and what's the difference
 between static structs and nonstatic ones when they are defined inside a
 function.

 Bye,
 bearophile
I guess I should read how tepmlates work. Tween needs access to unittest.Point2D to be instanciated but Point2D is local?
Apr 09 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Alexandr Druzhinin:

 I guess I should read how tepmlates work.
No templates are involved in this code. Bye, bearophile
Apr 09 2014
parent Alexandr Druzhinin <drug2004 bk.ru> writes:
09.04.2014 15:19, bearophile пишет:
 Alexandr Druzhinin:

 I guess I should read how tepmlates work.
No templates are involved in this code. Bye, bearophile
I mean that nested struct Point2D has additional pointer to frame and when compiler tries to instantiate template function 'tweet' it can't do it because in general it doesn't know about this additinal pointer to frame? And making it static I make it a general structure without additions and in this case compiler manages to instantiate 'tweet'?
Apr 09 2014