dcollections ArrayList pb with mixin template

2010-07-01 Thread BLS
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


Re: dcollections ArrayList pb with mixin template

2010-07-01 Thread Steven Schveighoffer

On Thu, 01 Jul 2010 15:36:53 -0400, BLS windev...@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


Re: dcollections ArrayList pb with mixin template

2010-07-01 Thread BLS

On 01/07/2010 22:59, Steven Schveighoffer wrote:

On Thu, 01 Jul 2010 15:36:53 -0400, BLS windev...@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


Re: dcollections ArrayList pb with mixin template

2010-07-01 Thread bearophile
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


Re: dcollections ArrayList pb with mixin template

2010-07-01 Thread BLS

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


Re: Mixing operations with signed and unsigned types

2010-07-01 Thread bearophile
Stewart Gordon:
 That code needs to be fixed?  My point was that being forced to use 
 signed types for values that cannot possibly be negative doesn't to me 
 constitute fixing anything.

Yes, in my opinion it needs to be fixed. Using unsigned integers in D is a 
hazard, so if you use them where they are not necessary (and representing 
positive-only values is often not one of such cases) then you are doing 
something wrong, or doing premature optimization. Using a unsigned value to 
represent a positive-only value is not going to increase your program safety as 
it happens for example in Delphi, in D it decreases your program resilience.

Using size_t and uint in your code where you can use an int is something that 
needs to be fixed, in my opinion. Normal D programmers writing very mundane 
code must not be forced to face unsigned values every few lines of code. 
Unsigned values in D are quite bug-prone, so the language has to avoid putting 
them on your plate every time you want to write some code. You need to be free 
to use them when you want, but it's better for you to use them only when 
necessary.

Unsigned values have some purposes, like representing bit fields, representing 
very large integers (over signed values range) when you are optimizing your 
code and with your profiler you have found a hot spot and you want to reduce 
space used or increase performance, to work with bitwise operators, to work 
with bit fields, and few more. But letting all programmers, even D newbies mess 
with unsigned values every time they want to use an array length is something 
that will cause a very large number of bugs and wasted programming time in 
future D programs. You will need a hard evidence to convince me this is false.

If you want to make your D code a bit more safe you have to write code like:
cast(int)somearray_.length - degree
because if you write more normal expressions like:
somearray_.length - degree
You can quickly put some bugs in your code :-) I have written something like 
300_000 lines of D code so far, and I have found a good number of 
unsigned-derived bugs in my code. Good luck with your code.

And by the way, in C# you have ways to use unsigned values, but I think array 
lengths and indexes are signed. Maybe they know better than Walter and you 
about this design detail.

Bye,
bearophile


Re: dcollections ArrayList pb with mixin template

2010-07-01 Thread bearophile
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