www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - dcollections ArrayList pb with mixin template

reply BLS <windevguy hotmail.de> writes:
Hi, I have a problem with a mixin template. More exact with an 
Arraylist!T within a mixin template.
Given.
void main() {
	auto p = new Person("Hans", 32);
	p ~= new Person("Steve", 40);
	p ~= new Person("Bjoern", 101);
}	

class Person {
	private	string _name;
	private uint _age;
	
	mixin TLinkList;
	
	this(string name, uint age) {
		this._name = name;
		this._age = age;
	}
}
	
mixin template TLinkList() {
	alias typeof(this) T;
	alias ArrayList!T TList;
	
	T[] pa;
	auto pl = new TList(pa);  // This does not work !
	void opCatAssign(T v) {
		pa ~= v;
	}	
}
Error: non-constant expression new ArrayList(pa)	main.d	

Ideas ?
Thanks Bjoern
Jul 01 2010
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 01 Jul 2010 15:36:53 -0400, BLS <windevguy hotmail.de> wrote:

 Hi, I have a problem with a mixin template. More exact with an  
 Arraylist!T within a mixin template.
 Given.
 void main() {
 	auto p = new Person("Hans", 32);
 	p ~= new Person("Steve", 40);
 	p ~= new Person("Bjoern", 101);
 }	

 class Person {
 	private	string _name;
 	private uint _age;
 	
 	mixin TLinkList;
 	
 	this(string name, uint age) {
 		this._name = name;
 		this._age = age;
 	}
 }
 	
 mixin template TLinkList() {
 	alias typeof(this) T;
 	alias ArrayList!T TList;
 	
 	T[] pa;
 	auto pl = new TList(pa);  // This does not work !
 	void opCatAssign(T v) {
 		pa ~= v;
 	}	
 }
 Error: non-constant expression new ArrayList(pa)	main.d	

 Ideas ?
 Thanks Bjoern
I'm thinking it has to do with you trying to create a member with that line. I think a member initializer has to be a constant expression, like int i = 1. Anything else has to be done in the constructor. This kinda sucks, because you can't initialize members with their defaults where you declare them, but it's the way D works. -Steve
Jul 01 2010
next sibling parent BLS <windevguy hotmail.de> writes:
On 01/07/2010 22:59, Steven Schveighoffer wrote:
 On Thu, 01 Jul 2010 15:36:53 -0400, BLS <windevguy hotmail.de> wrote:

 Hi, I have a problem with a mixin template. More exact with an
 Arraylist!T within a mixin template.
 Given.
 void main() {
 auto p = new Person("Hans", 32);
 p ~= new Person("Steve", 40);
 p ~= new Person("Bjoern", 101);
 }

 class Person {
 private string _name;
 private uint _age;

 mixin TLinkList;

 this(string name, uint age) {
 this._name = name;
 this._age = age;
 }
 }

 mixin template TLinkList() {
 alias typeof(this) T;
 alias ArrayList!T TList;

 T[] pa;
 auto pl = new TList(pa); // This does not work !
 void opCatAssign(T v) {
 pa ~= v;
 }
 }
 Error: non-constant expression new ArrayList(pa) main.d

 Ideas ?
 Thanks Bjoern
I'm thinking it has to do with you trying to create a member with that line. I think a member initializer has to be a constant expression, like int i = 1. Anything else has to be done in the constructor. This kinda sucks, because you can't initialize members with their defaults where you declare them, but it's the way D works. -Steve
Thanks for the feedback Steve. IMHO it should work.. One reason is that C.E. Miller has created a Circularly-linked list module, containing a portable linked list template mixin. Indeed Christophers implementation is different in that the LinkList is part of the mixin template... http://www.dprogramming.com/list.php 2) Commenting the auto pl = new TList() line out makes the snippet work. mixin template TLinkList() { alias typeof(this) T; alias ArrayList!T TList; T[] pa; //auto pl = new TList(pa); // NOW IT WORKS ! void opCatAssign(T v) { pa ~= v; } Well I am a D noob. Have to investigate a bit more, and of course any help is welcome :) Bjoern
Jul 01 2010
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Steven Schveighoffer:
 I think a member initializer has to be a constant expression, like int i =  
 1.  Anything else has to be done in the constructor.
There are the static constructors too, for modules, structs, classes. Bye, bearophile
Jul 01 2010
next sibling parent reply BLS <windevguy hotmail.de> writes:
On 02/07/2010 00:26, bearophile wrote:
 Steven Schveighoffer:
 I think a member initializer has to be a constant expression, like int i =
 1.  Anything else has to be done in the constructor.
There are the static constructors too, for modules, structs, classes. Bye, bearophile
Hi bearophile, I don't understand (in this context) . Can you please elaborate a bit more ? thanks bjoern
Jul 01 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
BLS:
 I don't understand (in this context) . Can you please elaborate a bit more ?
I have not shown you code because I don't understand your context. But you can put inside static this() {...} code that can't be run statically, like the initialization of a run-time thing. Bye, bearophile
Jul 01 2010
parent BLS <windevguy hotmail.de> writes:
On 02/07/2010 00:47, bearophile wrote:
 BLS:
 I don't understand (in this context) . Can you please elaborate a bit more ?
I have not shown you code because I don't understand your context. But you can put inside static this() {...} code that can't be run statically, like the initialization of a run-time thing. Bye, bearophile
Indeed, the sample snippet was bad 'cause I don't use static. mixin template TLinkList() { alias typeof(this) T; //alias LinkList!T TList; STATIC T[] pa; //static TList list; //= new TList(pa); // This does not work ! void opCatAssign(T v) { pa ~= v; } ... } // every struct respective class like class x { // enhanced with mixin TlinkList } can be used like every other linked list. (stack, queue, whatever) well, at least this is my intention. bjoern
Jul 02 2010
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 01 Jul 2010 18:26:06 -0400, bearophile <bearophileHUGS lycos.com>  
wrote:

 Steven Schveighoffer:
 I think a member initializer has to be a constant expression, like int  
 i =
 1.  Anything else has to be done in the constructor.
There are the static constructors too, for modules, structs, classes.
static constructors don't help initialize member variables. -Steve
Jul 02 2010