6

I'm having some trouble understanding how to deal with ambiguity of constructors in D.

struct mydta {
    int a = 2;
    int b = 3;

    this(int c) {
        a = c / 2;
        b = c * 2;
    }
    this(float c) {
        a = cast(int) c / 2;
        b = cast(int) c * 2;
    }

    static mydta afvec = mydta(4.3);
    static mydta aivec = mydta(5);
}
  • afvec has the data values 2 and 8.
  • aivec has the data values 5 and 3.

This means that afvec called this(float c) as expected from the syntax.

However aivec has done an assignment similar to aivec.a = 5.

I extrapolated on this to find that the following is legal in the above: aivec = mydta(5, 4); giving aivec the values 5 and 4 respectively.

Any ideas how to bypass this implicit initialization so I can access my constructor: this(int c)?

hiddensunset4
  • 5,825
  • 3
  • 39
  • 61
  • A float could also be 5 so it doesn't know which constructor to use. Try adding a cast to one of them. – Kevin May 31 '11 at 07:44
  • That is not the problem, the problem is if I use an int, it calls some implicit initialization instead of the `this(int c)` constructor I want it to call. – hiddensunset4 May 31 '11 at 07:48

1 Answers1

5

That looks like a bug to me.

If you do this:

mydta foo = mydta(5);
writeln(foo.b);

You get 10 as expected. There were a lot of changes to CTFE for version 2.053, so something may have been broken as a result. Please post it as a bug at the D issue tracking system.

Peter Alexander
  • 53,344
  • 14
  • 119
  • 168