Thread overview
opCmp and unittest as a nested function
Apr 09, 2014
Alexandr Druzhinin
Apr 09, 2014
bearophile
Apr 09, 2014
bearophile
Apr 09, 2014
Alexandr Druzhinin
Apr 09, 2014
Alexandr Druzhinin
Apr 09, 2014
bearophile
Apr 09, 2014
Alexandr Druzhinin
Apr 09, 2014
bearophile
Apr 09, 2014
Alexandr Druzhinin
April 09, 2014
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.
April 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
April 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
April 09, 2014
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?
April 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

April 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
April 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,
> bearophile
I guess I should read how tepmlates work. Tween needs access to unittest.Point2D to be instanciated but Point2D is local?
April 09, 2014
Alexandr Druzhinin:

> I guess I should read how tepmlates work.

No templates are involved in this code.

Bye,
bearophile
April 09, 2014
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'?