www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Multiple subtyping

reply Joel Christensen <joelcnz gmail.com> writes:
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
Aug 25 2011
next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
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.
Aug 25 2011
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
with doesn't work properly with alias this, I've already filed this in
bugzilla though.
Aug 26 2011