Thread overview
Multiple subtyping
Aug 26, 2011
Joel Christensen
Aug 26, 2011
Timon Gehr
Aug 26, 2011
Andrej Mitrovic
August 26, 2011
Hi,

Has anyone had much experience with multiple subtyping.

//Org: based on page 232 (6.13.1) in The D Programming Language book
//#save(); without storeShape does not work
import std.stdio;

class Shape {
	void shape() {
		writeln( "Shape" );
	}
}

class DataBase {
	void save() {
		writeln( "I'm the top - save" );
	}
}

class StoreShape : Shape {
	private class MyDataBase : DataBase {
		override void save() {
			super.save();
			writeln( "Data base: save" );
		}
	}
	
	private MyDataBase _store;
	alias _store this;
	
	this() {
		_store = this.new MyDataBase;
	}
}

void main() {
	auto storeShape = new StoreShape;
	
	with( storeShape ) {
		shape();
		storeShape.save(); //#save(); without storeShape. does not work
	}
}

- Joel

August 26, 2011
On 08/26/2011 05:45 AM, Joel Christensen wrote:
> Hi,
>
> Has anyone had much experience with multiple subtyping.
>
> //Org: based on page 232 (6.13.1) in The D Programming Language book
> //#save(); without storeShape does not work
> import std.stdio;
>
> class Shape {
> void shape() {
> writeln( "Shape" );
> }
> }
>
> class DataBase {
> void save() {
> writeln( "I'm the top - save" );
> }
> }
>
> class StoreShape : Shape {
> private class MyDataBase : DataBase {
> override void save() {
> super.save();
> writeln( "Data base: save" );
> }
> }
>
> private MyDataBase _store;
> alias _store this;
>
> this() {
> _store = this.new MyDataBase;
> }
> }
>
> void main() {
> auto storeShape = new StoreShape;
>
> with( storeShape ) {
> shape();
> storeShape.save(); //#save(); without storeShape. does not work
> }
> }
>
> - Joel
>

That is a compiler bug. Alias this does not work properly yet. (at the moment, there can also be only one alias this.)

The reason why it does not work is that the compiler rewrites the with ( ) { } in funny ways.



August 26, 2011
with doesn't work properly with alias this, I've already filed this in bugzilla though.