Thread overview
alias this leads to compilation error in one of two similar contexts
Nov 30, 2013
Carl Sturtivant
Nov 30, 2013
Carl Sturtivant
Dec 02, 2013
Kenji Hara
Dec 04, 2013
Carl Sturtivant
Dec 01, 2013
Ali Çehreli
November 30, 2013
The following is boiled down from a real world context. I'm using dmd 2.064.2 Windows. Can someone please explain what's going on.

================

struct my_integer {
    int val = 99;
    alias val this;
    //this( int n) { val = n; }
}

struct blah {
    my_integer Integer;

    this( int n) { Integer = n; }
    ref blah opAssign( int n) { Integer = n; return this; }
}

================

The constructor in blah does not compile unless the constructor in my_integer is uncommented. Yet the opAssign function in blah compiles just fine.

The error message is
 Error: cannot implicitly convert expression (n) of type int to my_integer
and the line causing it is blah's constructor.

This behavior did not exist with dmd 2.063.2 (Windows) --- I reverted to check this.

November 30, 2013
I just confirmed the same behavior on Ubuntu amd64. dmd 2.063.2 compiles the example and dmd 2.064.2 produces the same error as the Windows 32 bit version.
December 01, 2013
On 11/30/2013 02:18 PM, Carl Sturtivant wrote:
>
> The following is boiled down from a real world context. I'm using dmd
> 2.064.2 Windows. Can someone please explain what's going on.
>
> ================
>
> struct my_integer {
>      int val = 99;
>      alias val this;
>      //this( int n) { val = n; }
> }
>
> struct blah {
>      my_integer Integer;
>
>      this( int n) { Integer = n; }
>      ref blah opAssign( int n) { Integer = n; return this; }
> }
>
> ================
>
> The constructor in blah does not compile unless the constructor in
> my_integer is uncommented. Yet the opAssign function in blah compiles
> just fine.
>
> The error message is
>   Error: cannot implicitly convert expression (n) of type int to my_integer
> and the line causing it is blah's constructor.
>
> This behavior did not exist with dmd 2.063.2 (Windows) --- I reverted to
> check this.
>

At least the discrepancy warrants a bug report:

  https://d.puremagic.com/issues/

Ali

December 02, 2013
On Saturday, 30 November 2013 at 22:28:14 UTC, Carl Sturtivant wrote:
>
> I just confirmed the same behavior on Ubuntu amd64. dmd 2.063.2 compiles the example and dmd 2.064.2 produces the same error as the Windows 32 bit version.

This is intended behavior change from 2.064.

http://dlang.org/class#field-init

struct my_integer {
    int val = 99;
    alias val this;
}
void test() {
  //my_interger num = 1;  // NG
    my_interger num = my_interger(1);  // OK
    // 'alias this' has no effect for object initialization
}
struct blah {
    my_integer num;

    this(int n) {
      //num = n;  // also NG, my_integer cannot *initialize* by int value
        num = my_interger(n);  // OK
    }
}

Kenji Hara
December 04, 2013
> This is intended behavior change from 2.064.
>
> http://dlang.org/class#field-init
> [...]
> Kenji Hara

Interesting, useful, and a surprise. Thank you!