Re: inheriting ctors?

2010-08-07 Thread div0
On 06/08/2010 22:57, Andrej Mitrovic wrote: // Offtopic Template errors are so hard to grasp, most of the time it's best to just ignore them and take some logical steps to fix the errors. At least that's in my case true.. lol, that's true. I've been basing out c++ & d template code for years

Array Operations and type inference

2010-08-07 Thread simendsjo
I'm new to D2, so please.. :) The spec on Array Operations, http://www.digitalmars.com/d/2./arrays.html says: """A vector operation is indicated by the slice operator appearing as the lvalue of an =, +=, -=, *=, /=, %=, ^=, &= or |= operator.""" The following tests works fine as I expected:

std.variant and const

2010-08-07 Thread Lutger
Variant in phobos doesn't work well with being const, toHash and opEquals for example are not marked as const methods. I have tried to work around it with careful casting, but it is too unsafe and just too complex for something that is supposed to make life easier. Does anybody know if this i

Slicing to convert pointers to bound arrays

2010-08-07 Thread simendsjo
I understand this is an unsafe operation, I just have a quick question. Here, b points outside a's memory, so I get garbage. { int[] a = [3,3,3]; auto p = a.ptr; auto b = p[0..3]; assert(b == [3,3,3]); b = p[

Re: Array Operations and type inference

2010-08-07 Thread Mafi
Am 07.08.2010 14:10, schrieb simendsjo: I'm new to D2, so please.. :) The spec on Array Operations, http://www.digitalmars.com/d/2./arrays.html says: """A vector operation is indicated by the slice operator appearing as the lvalue of an =, +=, -=, *=, /=, %=, ^=, &= or |= operator.""" The follo

Default constructor for structs

2010-08-07 Thread Peter Alexander
Since default constructors for structs are not allowed, how do I go about making all the elements of this Matrix struct default to zero? (floats default to nan by default). struct Matrix(int M, int N) { float[M][N] elements; } Matrix(3, 3) m; assert(m[0][0] == 0); ?

Re: Default constructor for structs

2010-08-07 Thread bearophile
Peter Alexander: > Since default constructors for structs are not allowed, how do I go > about making all the elements of this Matrix struct default to zero? > (floats default to nan by default). This doesn't work, I don't know why: float[M][N] elements = 0.0; A bad looking solution: struct Ma

Re: Slicing to convert pointers to bound arrays

2010-08-07 Thread bearophile
simendsjo: > When I do the same with a static array, I get 0. But this is just > actually garbage, right? It might contain other data, and not always 0? Generally it contains "garbage". In string literals there is one more item that's empty (zero). I don't know if normal fixed-sized arrays too h

Re: Slicing to convert pointers to bound arrays

2010-08-07 Thread div0
On 07/08/2010 13:45, simendsjo wrote: When I do the same with a static array, I get 0. But this is just actually garbage, right? It might contain other data, and not always 0? Yes. It's entirely undefined what will happen, you might find that the extra bit of memory you try and access is a no

Re: Default constructor for structs

2010-08-07 Thread Peter Alexander
On 7/08/10 3:44 PM, bearophile wrote: Peter Alexander: Bye, bearophile Thanks for your detailed reply. However, I'm not particularly satisfied with any of those :( I have already gone with the single parameter init constructor in my code until I find something better, but really, I just wa

Re: Operator overloading problem

2010-08-07 Thread Blonder
Is there a difference (performance) or something else between the two solutions?

Is "is" the same as ptr == ptr for arrays?

2010-08-07 Thread simendsjo
Is the following equalent? int[] a; int[] b = a; assert(a is b); assert(a.ptr == b.ptr);

Re: Is "is" the same as ptr == ptr for arrays?

2010-08-07 Thread Peter Alexander
On 7/08/10 4:33 PM, simendsjo wrote: Is the following equalent? int[] a; int[] b = a; assert(a is b); assert(a.ptr == b.ptr); No. (a is b) implies (a.ptr == b.ptr) but (a.ptr == b.ptr) does not imply (a is b) For example: int[] a = [1, 2, 3]; int[] b = a[0..1]; Here, a.ptr == b.ptr, but

Re: Is "is" the same as ptr == ptr for arrays?

2010-08-07 Thread simendsjo
On 07.08.2010 18:04, Peter Alexander wrote: On 7/08/10 4:33 PM, simendsjo wrote: Is the following equalent? int[] a; int[] b = a; assert(a is b); assert(a.ptr == b.ptr); No. (a is b) implies (a.ptr == b.ptr) but (a.ptr == b.ptr) does not imply (a is b) For example: int[] a = [1, 2, 3]; i

Re: Array Operations and type inference

2010-08-07 Thread Mafi
Hey, here Mafi again, I thought about your snippets and here's what I have. Am 07.08.2010 14:10, schrieb simendsjo: { double[3] a = [1,1,1]; double[3] b; b[] = a[] + 3; assert(a == [1,1,1]); assert(b == [4,4,4]); } { double[3] a = [1,1,1]; auto b = a; b[] = a[] + 3; assert(a == [1,1,1]); assert

Re: Default constructor for structs

2010-08-07 Thread rouge
Hi, I'm a newbie, hope the attached example works for you. Peter Alexander wrote: Since default constructors for structs are not allowed, how do I go about making all the elements of this Matrix struct default to zero? (floats default to nan by default). struct Matrix(int M, int N) { floa

Static arrays passed by value..?

2010-08-07 Thread simendsjo
The spec for array says: Static arrays are value types. Unlike in C and D version 1, static arrays are passed to functions by value. Static arrays can also be returned by functions. I don't get the "static arrays are passed to functions by value" part. Here I am passing in a static and dynam

Re: Default constructor for structs

2010-08-07 Thread rouge
oops :p correction: in template Zero const float[M][N] ==> const T[M][N] rouge wrote: Hi, I'm a newbie, hope the attached example works for you. Peter Alexander wrote: Since default constructors for structs are not allowed, how do I go about making all the elements of this Matrix struct defaul

Re: Operator overloading problem

2010-08-07 Thread div0
On 07/08/2010 16:25, Blonder wrote: Is there a difference (performance) or something else between the two solutions? I doubt it, templates are applied at compile time in the front end of the compiler so the generated code should be the same. The if style syntax allows much more comprehensive

Re: Static arrays passed by value..?

2010-08-07 Thread Lionello Lunesu
On 2010-08-07 9:26, simendsjo wrote: The spec for array says: Static arrays are value types. Unlike in C and D version 1, static arrays are passed to functions by value. Static arrays can also be returned by functions. I don't get the "static arrays are passed to functions by value" part. Here

template fiddling

2010-08-07 Thread Blonder
Hello, I am trying to understand the template mechanism in D, but I don't get it working. I want to compute the dot product as follows: int[] a = [1, 2, 3]; int[] b = [5, 6, 7]; double dd = ???.dot_product( a, b ); with classes like this: class DotProduct(DIM, T) {

Re: template fiddling

2010-08-07 Thread Mafi
Am 07.08.2010 18:40, schrieb Blonder: Hello, I am trying to understand the template mechanism in D, but I don't get it working. I want to compute the dot product as follows: int[] a = [1, 2, 3]; int[] b = [5, 6, 7]; double dd = ???.dot_product( a, b ); with classes li

Re: Is "is" the same as ptr == ptr for arrays?

2010-08-07 Thread Simen kjaeraas
simendsjo wrote: Ok, thanks. Does this mean this equivalent then? int[] a = [1,2,3]; int[] b = a[0..1]; assert(a !is b); assert(a.ptr == b.ptr && a.length == b.length); Well, no. But what you probably mean, is. ( a is b ) == ( a.ptr == b.ptr && a.length == b.length ) -- Simen

Re: Static arrays passed by value..?

2010-08-07 Thread Simen kjaeraas
simendsjo wrote: The spec for array says: Static arrays are value types. Unlike in C and D version 1, static arrays are passed to functions by value. Static arrays can also be returned by functions. I don't get the "static arrays are passed to functions by value" part. Here I am passing

itfi limitation or bug on my part?

2010-08-07 Thread Brad Roberts
module test; void main() { int[] foos; func1(foos); } void func1(T)(const T[] foos) { T afoo; func2(foos, afoo); } void func2(T)(const T[] foos, out T afoo) { } $ dmd -c test.d test.d(12): Error: template test.func2(T) does not match any function template declaration test.d(12):

Re: template fiddling

2010-08-07 Thread Blonder
Hello, the template!(...) mechanism I understand. But I think I need the two classes, the first is the primary template and the second the partial specialization (this is the end of the "loop"), or can I do this in D with functions? The normal computation of the dot product is normally done in a

Re: Array Operations and type inference

2010-08-07 Thread Don
Mafi wrote: Hey, here Mafi again, I thought about your snippets and here's what I have. Am 07.08.2010 14:10, schrieb simendsjo: { // Like the previous example, but with dynamic arrays.. double[] a = [1,1,1]; auto b = a; assert(a is b); b = a[] + 3; assert(a == [1,1,1]); //writeln(b); // acces

Re: itfi limitation or bug on my part?

2010-08-07 Thread Don
Brad Roberts wrote: module test; void main() { int[] foos; func1(foos); } void func1(T)(const T[] foos) { T afoo; func2(foos, afoo); } void func2(T)(const T[] foos, out T afoo) { } $ dmd -c test.d test.d(12): Error: template test.func2(T) does not match any function template d

Re: itfi limitation or bug on my part?

2010-08-07 Thread Brad Roberts
On 8/7/2010 12:42 PM, Don wrote: > Brad Roberts wrote: >> module test; >> >> void main() >> { >> int[] foos; >> func1(foos); >> } >> >> void func1(T)(const T[] foos) >> { >> T afoo; >> func2(foos, afoo); >> } >> >> void func2(T)(const T[] foos, out T afoo) >> { >> } >> >> $ dmd -c t

Re: Do I have to cast double to cdouble everywhere?

2010-08-07 Thread Don
John Travers wrote: Hi, I'm investigating if D would be useful to me as a numerical programming language to replace my current mix of fortran and python. Welcome! There are quite a few of us who are numerical programmers here, and it's one of D's target areas. I'm stuck with a problem whi

Re: template fiddling

2010-08-07 Thread Dmitry Olshansky
On 07.08.2010 23:04, Blonder wrote: Hello, the template!(...) mechanism I understand. But I think I need the two classes, the first is the primary template and the second the partial specialization (this is the end of the "loop"), or can I do this in D with functions? The normal computation of

Re: template fiddling

2010-08-07 Thread Mafi
Am 07.08.2010 21:04, schrieb Blonder: Hello, the template!(...) mechanism I understand. But I think I need the two classes, the first is the primary template and the second the partial specialization (this is the end of the "loop"), or can I do this in D with functions? The normal computation o

Re: template fiddling

2010-08-07 Thread bearophile
Dmitry Olshansky: > Probably, this could be a starting point (BTW In D you can write > templates very easy): > > T dot_product(size_t N, T)(T[] a, T[] b){ > static if (N == 1){ > return a[0] * b[0]; > }else{ > return a[0] * b[0] + dot_product!(N-1)(a[1..$],b[1..$]); >

Re: std.variant and const

2010-08-07 Thread Jonathan M Davis
On Saturday 07 August 2010 05:42:53 Lutger wrote: > Variant in phobos doesn't work well with being const, toHash and opEquals > for example are not marked as const methods. I have tried to work around > it with careful casting, but it is too unsafe and just too complex for > something that is suppo

Re: std.variant and const

2010-08-07 Thread Lutger
Jonathan M Davis wrote: > On Saturday 07 August 2010 05:42:53 Lutger wrote: >> Variant in phobos doesn't work well with being const, toHash and opEquals >> for example are not marked as const methods. I have tried to work around >> it with careful casting, but it is too unsafe and just too complex

Re: Is "is" the same as ptr == ptr for arrays?

2010-08-07 Thread BCS
Hello Simen, simendsjo wrote: Ok, thanks. Does this mean this equivalent then? int[] a = [1,2,3]; int[] b = a[0..1]; assert(a !is b); assert(a.ptr == b.ptr && a.length == b.length); Well, no. both asserts will fail (a.length == 3 != b.length == 1) But what you probably mean, is. ( a is

Re: Static arrays passed by value..?

2010-08-07 Thread BCS
Hello simendsjo, The spec for array says: Static arrays are value types. Unlike in C and D version 1, static arrays are passed to functions by value. Static arrays can also be returned by functions. I don't get the "static arrays are passed to functions by value" part. Here I am passing in a s

palindrome function

2010-08-07 Thread ex
Hello! I'm learning D and I was wondering on what can be optimized of my implementation of a palindrome function: bool isPalindrome(int num) { string s = text(num); int len = s.length; for (int i = 0; i < len / 2; ++i) { if (s[i] != s[len - 1 - i]) { return false;

Re: palindrome function

2010-08-07 Thread ex
Sorry I forg to paste my last version" bool isPalindrome(int num) { string s = text(num); int length = s.length; int limit = length / 2; for (int i = 0; i < limit; ++i) { if (s[i] != s[$ - 1 - i]) { return false; } } return true; }

Re: palindrome function

2010-08-07 Thread Jonathan M Davis
On Saturday 07 August 2010 16:56:08 ex wrote: > Hello! > I'm learning D and I was wondering on what can be optimized of my > implementation of a palindrome function: > > bool isPalindrome(int num) { > string s = text(num); > int len = s.length; > for (int i = 0; i < len / 2; ++i) { >

Re: palindrome function

2010-08-07 Thread Jonathan M Davis
As a side note, I'd suggest making your function take a string of some kind, if not outright making it a template to deal with multiple string types. That way, you can check for a palindrome regardless of whether you're dealing with numbers, and if you need to do it with a number, you just conve

Re: palindrome function

2010-08-07 Thread bearophile
ex: Welcome to D :-) Few more comments beside the ones written by Jonathan M Davis. > I'm learning D and I was wondering on what can be optimized of my > implementation of a palindrome function: See below. > 2) Does using a variable to store the string length is more efficient than > calling th