Re: Getting equivalent elements in a range/array

2011-05-07 Thread Andrej Mitrovic
Fantastic work, thanks! I'll look into more detail tomorrow, but it looks good so far. Just added a function helper and made the struct typed: import std.array; import std.range; struct EquivalentElements(T) { T range; T front_; this(T range) { this.range = range; this.

Re: Getting equivalent elements in a range/array

2011-05-07 Thread Ali Çehreli
On 05/07/2011 09:07 PM, Andrej M. wrote: I want to turn this: auto arr = [1, 1, 2, 3, 4, 4]; into this: auto arr2 = [[1, 1], [2], [3], [4, 4]]; I want an array of arrays of the same elements. Lazy or not, I don't care. I thought I could get away with this inside some while loop: auto equals =

Getting equivalent elements in a range/array

2011-05-07 Thread Andrej M.
I want to turn this: auto arr = [1, 1, 2, 3, 4, 4]; into this: auto arr2 = [[1, 1], [2], [3], [4, 4]]; I want an array of arrays of the same elements. Lazy or not, I don't care. I thought I could get away with this inside some while loop: auto equals = array(filter!"a == b"(arr)); arr = arr[equa

Shouldn't duplicate functions be caught by DMD?

2011-05-07 Thread Andrej Mitrovic
I'm not talking about function overloading, but functions with the same parameters inside the same class definition: class Foo { int foo(int i) { return 1; } int foo(int i) { return 1; } void bar() { foo(1); } } void main(

Re: Cannot interpret struct at compile time

2011-05-07 Thread Robert Clipsham
On 08/05/2011 00:39, Andrej Mitrovic wrote: One simplistic solution is to use alias this to simulate the same type: struct Foo { int x, y; } string structClone(T)() { return "struct " ~ T.stringof ~ "_ { " ~ T.stringof ~ " _inner; alias _inner this; this(T..

Re: Cannot interpret struct at compile time

2011-05-07 Thread Andrej Mitrovic
One simplistic solution is to use alias this to simulate the same type: struct Foo { int x, y; } string structClone(T)() { return "struct " ~ T.stringof ~ "_ { " ~ T.stringof ~ " _inner; alias _inner this; this(T...)(T t) { _inner = typeof(_inner)(t); } };"; } vo

Re: Cannot interpret struct at compile time

2011-05-07 Thread Robert Clipsham
On 07/05/2011 23:36, Andrej Mitrovic wrote: Not too sure, CTFE is a pain in the ass sometimes. What exactly are you trying to do, print field names in a custom way? No, I have a struct that I don't have access to in the scope I'm in, I do however have its type - by using the above, I can creat

Re: Cannot interpret struct at compile time

2011-05-07 Thread Andrej Mitrovic
Not too sure, CTFE is a pain in the ass sometimes. What exactly are you trying to do, print field names in a custom way? I have this piece of code I use for printing values, maybe you can customize it for own your needs: import std.stdio; import std.conv; import std.algorithm; struct Foo { i

Cannot interpret struct at compile time

2011-05-07 Thread Robert Clipsham
Hey all, I was wondering if anyone could enlighten me as to why the following code does not compile (dmd2, latest release or the beta): struct Foo { int a; } string test() { string str = "struct " ~ Foo.stringof ~ "_{"; foreach (j, f; Foo.tupleof) {

Re: int or size_t ?

2011-05-07 Thread Andrej Mitrovic
Edit: I just saw you've already figured this out. :)

Re: int or size_t ?

2011-05-07 Thread Andrej Mitrovic
Actually my example was bad. What I wanted to say is that size_t will be 64bit on 64bit platforms while int will stay 32bit. Another difference is that size_t is unsigned. So it's bad to use int even if you're sure you're only going to compile only on 32bit platforms. Here's the relevant definitio

Re: int or size_t ?

2011-05-07 Thread %u
size_t val1 = int.max+1; int val2 = int.max+1; writeln(val1); // 2147483648 writeln(val2); // -2147483648 very clear example thanks you both

Re: int or size_t ?

2011-05-07 Thread Andrej Mitrovic
void main() { size_t val = int.max+1; int val2 = val; writeln(val2); } writes -2147483648 That should give you a hint.

Re: int or size_t ?

2011-05-07 Thread bearophile
%u: > In Patterns of Human Error, the slide 31 point that you should replce int with > size_t > why that consider an error ? If T is a byte and the array size is 5 billion items, on 64 bit systems...? In the little find() function you compare it with the length, that's a size_t. Someone else wi

int or size_t ?

2011-05-07 Thread %u
In Patterns of Human Error, the slide 31 point that you should replce int with size_t why that consider an error ?