digitalmars.D.learn - opCmp and unittest as a nested function
- Alexandr Druzhinin (7/7) Apr 09 2014 Hello!
- bearophile (4/8) Apr 09 2014 Try to use "static struct" instead of a "struct".
- bearophile (18/18) Apr 09 2014 And you need to use opEquals:
- Alexandr Druzhinin (3/21) Apr 09 2014 Thanks!
- Alexandr Druzhinin (2/9) Apr 09 2014 Thank you! It works. But where can I read about this issue?
- bearophile (7/8) Apr 09 2014 Reading about this "issue" is not good. What you can read about
- Alexandr Druzhinin (3/9) Apr 09 2014 I guess I should read how tepmlates work. Tween needs access to
- bearophile (4/5) Apr 09 2014 No templates are involved in this code.
- Alexandr Druzhinin (6/11) Apr 09 2014 I mean that nested struct Point2D has additional pointer to frame and
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
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
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
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
09.04.2014 13:45, bearophile пишет:Alexandr Druzhinin:Thank you! It works. But where can I read about this issue?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
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
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, bearophileI guess I should read how tepmlates work. Tween needs access to unittest.Point2D to be instanciated but Point2D is local?
Apr 09 2014
Alexandr Druzhinin:I guess I should read how tepmlates work.No templates are involved in this code. Bye, bearophile
Apr 09 2014
09.04.2014 15:19, bearophile пишет:Alexandr Druzhinin: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'?I guess I should read how tepmlates work.No templates are involved in this code. Bye, bearophile
Apr 09 2014