Re: Calculation differences between Debug and Release mode

2013-04-13 Thread Alexandr Druzhinin
I'm not sure, but I suspect this is because of 80-bit intermediary float point operation result. Its precision too excessive and gives us this inexpectible result. But when you use an intermediary variable this exessive intermediary result is rounded properly and you get what you expect. See he

class constructor

2013-04-13 Thread gedaiu
Hi, Why in D this expression does not call the class constructor? class A { int myVal; this(int val) { myVal = val; } } int main() { A myA = 8; } I would like to have a feature like this, because i want to create my own data type. I think it's possible, but i don't

Re: class constructor

2013-04-13 Thread Nicolas Guillemot
Classes are instanciated with new, structs are not. The following program compiles: class A { int myVal; this(int val) { myVal = val; } } struct B { int myVal; this(int val) { myVal = val; } } void main() {

Re: class constructor

2013-04-13 Thread gedaiu
On Saturday, 13 April 2013 at 07:57:30 UTC, Nicolas Guillemot wrote: Classes are instanciated with new, structs are not. The following program compiles: class A { int myVal; this(int val) { myVal = val; } } struct B { int myVal; this(in

How i can clear Associative Arrays

2013-04-13 Thread gedaiu
Hi, I have an associative array: string values[string], and i want to remove all the values from this array. I looked at the documentation here: http://dlang.org/hash-map.html and i can't see any method for this action. There is a nice way to remove the values, or I should use foreach? Than

Re: How i can clear Associative Arrays

2013-04-13 Thread Nicolas Guillemot
Hey gedaiu, I'm still a novice to D, but here are some solutions I found. They can probably be improved. 1) Assigning to it an empty map https://ideone.com/h7ffmD 2) Removing all entries https://ideone.com/E7k2WL My guess is that the first method is more efficient. I wish I knew how to do i

Re: How i can clear Associative Arrays

2013-04-13 Thread gedaiu
On Saturday, 13 April 2013 at 09:09:36 UTC, Nicolas Guillemot wrote: Hey gedaiu, I'm still a novice to D, but here are some solutions I found. They can probably be improved. 1) Assigning to it an empty map https://ideone.com/h7ffmD 2) Removing all entries https://ideone.com/E7k2WL My guess

Re: How i can clear Associative Arrays

2013-04-13 Thread Nicolas Guillemot
values = null; and it seems it works great. But, i don't know how correct is this... I was expecting to have a clear() method on array. Makes sense to me! Looks like the technique you described is explained here: http://ddili.org/ders/d.en/null_is.html Good find, thanks for sharing!

Re: How i can clear Associative Arrays

2013-04-13 Thread Andrej Mitrovic
On 4/13/13, gedaiu wrote: > looks great, but i cleared the array like this: > > values = null; That's not clearing the array, that's clearing the reference to the array. For example: void main() { int[int] hash; hash[1] = 1; auto hash2 = hash; // new reference hash = null; //

opIndex operators

2013-04-13 Thread gedaiu
Hi again! I don't understand why we have opIndex() and opIndexAssign()... can anyone explain this for me? Because in c++ there is only one operator overload for "[]" and it looks like this: Value& Value::operator[] (constValue offset) { return container[offset]; } and with this ope

Re: How i can clear Associative Arrays

2013-04-13 Thread gedaiu
On Saturday, 13 April 2013 at 09:52:45 UTC, Andrej Mitrovic wrote: On 4/13/13, gedaiu wrote: looks great, but i cleared the array like this: values = null; That's not clearing the array, that's clearing the reference to the array. For example: void main() { int[int] hash; hash[1]

Re: How i can clear Associative Arrays

2013-04-13 Thread Andrej Mitrovic
On 4/13/13, gedaiu wrote: > I know, that's why I am asking how i should do this... I think we should introduce a removeAll function for hashes. Either through Druntime or through a UFCS function that we could put in std.array or somewhere. Putting it in Druntime is probably the most efficient wa

Re: class constructor

2013-04-13 Thread bearophile
Nicolas Guillemot: Classes are instanciated with new, structs are not. structs are created with new if you want them on the heap. Bye, bearophile

Re: Calculation differences between Debug and Release mode

2013-04-13 Thread bearophile
Alexandr Druzhinin: I'm not sure, but I suspect this is because of 80-bit intermediary float point operation result. Maybe D uses higher precision FP values in some of those intermediate computations. In general float is useful if you have to store many of them, for storage reasons (reduce m

Re: opIndex operators

2013-04-13 Thread bearophile
On Saturday, 13 April 2013 at 09:53:01 UTC, gedaiu wrote: Hi again! I don't understand why we have opIndex() and opIndexAssign()... can anyone explain this for me? opIndex returns the value contained in the "array", while opIndexAssign does the opposite, it puts in your "array" the given va

Re: opIndex operators

2013-04-13 Thread gedaiu
Hi, thanks for the answer, but it's not the solution what i was expectig. What i want to create, is an array structure like the one from PHP where array levels are not fixed and the sintax to access rhe values is val[][] so returning a reference to a struct that have the same type as the curr

Re: opIndex operators

2013-04-13 Thread bearophile
gedaiu: What i want to create, is an array structure like the one from PHP where array levels are not fixed and the sintax to access rhe values is val[][] so returning a reference to a struct that have the same type as the current type is useful. there is a way to do this in D? Instead of

Re: Calculation differences between Debug and Release mode

2013-04-13 Thread Simen Kjaeraas
On Sat, 13 Apr 2013 08:07:39 +0200, Jeremy DeHaan wrote: In debug mode this works as expected. Let's say the radius is 50. getPoint(0) returns a vector that prints X: 50 Y: 0. For some reason, the same function will return a vector that prints X: 50 Y: 4.77673e-14. Now, 4.77673e-14 is a

Vector operations cannot be nothrow?

2013-04-13 Thread simendsjo
void f() nothrow { // Error: _arrayExpSliceAddass_i is not nothrow int[3] d; d[] += 3; }

Bit Array: which one?

2013-04-13 Thread Matej Nanut
Hello, I noticed there are two different packed bit array implementations in Phobos: the specialised version of std.container.Array and std.bitmanip.BitArray. Which one should be used? Thanks, Matej

Derived classes? Reflection

2013-04-13 Thread Tofu Ninja
Currently is there a way to get all of the subclasses of a class at compile time? It seems like something that should be possible using traits but I can't seems to see how. Something like class A{...} class B:A{...} class C:A{...} ... foreach(auto t ; getsubclass(A)) { ... } Any help wou

Re: Calculation differences between Debug and Release mode

2013-04-13 Thread Jeremy DeHaan
On Saturday, 13 April 2013 at 11:59:12 UTC, Simen Kjaeraas wrote: On Sat, 13 Apr 2013 08:07:39 +0200, Jeremy DeHaan wrote: In debug mode this works as expected. Let's say the radius is 50. getPoint(0) returns a vector that prints X: 50 Y: 0. For some reason, the same function will return a v

Re: Derived classes? Reflection

2013-04-13 Thread Andrej Mitrovic
On 4/13/13, Tofu Ninja wrote: > Currently is there a way to get all of the subclasses of a class > at compile time? There might be things like using .moduleinfo and traversing imports to find all classes, but I think separate compilation makes this unreliable. There is however BaseClassTuple in

Re: Derived classes? Reflection

2013-04-13 Thread Namespace
On Saturday, 13 April 2013 at 16:16:03 UTC, Tofu Ninja wrote: Currently is there a way to get all of the subclasses of a class at compile time? It seems like something that should be possible using traits but I can't seems to see how. Something like class A{...} class B:A{...} class C:A{...}

Re: Derived classes? Reflection

2013-04-13 Thread Tofu Ninja
Maybe this is helpfully: http://forum.dlang.org/thread/scgjnudclnwlbdqqd...@forum.dlang.org Oddly enough, I found that at about the same time you posted it.

Re: Derived classes? Reflection

2013-04-13 Thread Tofu Ninja
On Saturday, 13 April 2013 at 17:45:12 UTC, Tofu Ninja wrote: Maybe this is helpfully: http://forum.dlang.org/thread/scgjnudclnwlbdqqd...@forum.dlang.org Oddly enough, I found that at about the same time you posted it. Sadly this does not provide a compile time solution, only runtime.

Re: Vector operations cannot be nothrow?

2013-04-13 Thread bearophile
simendsjo: void f() nothrow { // Error: _arrayExpSliceAddass_i is not nothrow int[3] d; d[] += 3; } Currently those ops are not nothrow. I think they will eventually become nothrow... Bye, bearophile

Re: How i can clear Associative Arrays

2013-04-13 Thread Nicolas Guillemot
I think we should introduce a removeAll function for hashes. Either through Druntime or through a UFCS function that we could put in std.array or somewhere. How about .clear() for consistency with C++ containers?

Different behaviour of new and malloc

2013-04-13 Thread Namespace
I have a problem and maybe one you can explain, why this failure happen. Initial situation: I have a opengl texture and I want to copy the pixel. I store the original pixel in an ubyte pointer and allocate new pixel memory with: ubyte[] newPixel = new ubyte[this.width * this.height * this.dept

Re: Different behaviour of new and malloc

2013-04-13 Thread Namespace
Forget to say: Of course I use the .ptr property to load the newPixel into the texture, if I use the ubyte storage.

Re: Different behaviour of new and malloc

2013-04-13 Thread Ali Çehreli
On 04/13/2013 02:22 PM, Namespace wrote: > I have a opengl texture and I want to copy the pixel. I store the > original pixel in an ubyte pointer and allocate new pixel memory with: > ubyte[] newPixel = new ubyte[this.width * this.height * this.depth]; Make sure that you really do not need to mu

Re: Different behaviour of new and malloc

2013-04-13 Thread bearophile
Namespace: I have a problem and maybe one you can explain, why this failure happen. Consider using something like: auto newPixel = orgPixel[0 .. this.width * this.height * this.depth].dup; Bye, bearophile

Re: Different behaviour of new and malloc

2013-04-13 Thread Namespace
On Saturday, 13 April 2013 at 22:18:21 UTC, bearophile wrote: Namespace: I have a problem and maybe one you can explain, why this failure happen. Consider using something like: auto newPixel = orgPixel[0 .. this.width * this.height * this.depth].dup; Bye, bearophile Nice idea. That works

Strict definition of value types and reference types

2013-04-13 Thread Nicholas Smith
Two things that I can't seem to nail down a strict definition for are value types and reference types. I know very well their basic definitions: a variable of a value type holds its own data and assignment copies the data, and a variable of a reference type holds a pointer to its data and assig

Re: Strict definition of value types and reference types

2013-04-13 Thread Nicholas Smith
Re-wording my last paragraph because of incorrect wording: In essence, don't all variables act as structs, with different operator overloads to give different semantics? If so, how do we classify 'value type' versus 'reference type'? What if I have a struct whose members are a fixed-length arr

Re: Strict definition of value types and reference types

2013-04-13 Thread bearophile
Nicholas Smith: it is said that slices are reference types. Sure, slices hold references to an array, but they also carry their own data, which gives them value semantics. Slices are a small value, that contains in place a length and a pointer. The pointer refers to memory allocated elsewher

Re: Strict definition of value types and reference types

2013-04-13 Thread Nicholas Smith
On Sunday, 14 April 2013 at 02:51:35 UTC, bearophile wrote: slices are kind of hybrids between values and references. You can also think of them as fat references. When you program in D you must remember this nature of sliced, otherwise your code will not work... Bye, bearophile Ali Çehreli

Re: Strict definition of value types and reference types

2013-04-13 Thread Ali Çehreli
On 04/13/2013 08:46 PM, Nicholas Smith wrote: > On Sunday, 14 April 2013 at 02:51:35 UTC, bearophile wrote: >> slices are kind of hybrids between values and references. You can also >> think of them as fat references. When you program in D you must >> remember this nature of sliced, otherwise you

Re: How i can clear Associative Arrays

2013-04-13 Thread gedaiu
On Saturday, 13 April 2013 at 21:10:16 UTC, Nicolas Guillemot wrote: I think we should introduce a removeAll function for hashes. Either through Druntime or through a UFCS function that we could put in std.array or somewhere. How about .clear() for consistency with C++ containers? It looks