Re: Reuse object memory?

2015-04-21 Thread via Digitalmars-d-learn
On Monday, 20 April 2015 at 21:36:35 UTC, Ali Çehreli wrote: final class Foo { uint id; @nogc this(uint id) { this.id = id; } } C reuse(C, T...)(ref C old, T ctorParams) { import std.conv; import std.typetuple; enum objectSize = __traits(classInstanceSize, C

Re: Reuse object memory?

2015-04-20 Thread Namespace via Digitalmars-d-learn
On Monday, 20 April 2015 at 21:58:59 UTC, Ali Çehreli wrote: On 04/20/2015 02:44 PM, Namespace wrote: > Thank you. Do you mean this is worth a PR, to add this > functionality to Phobos? I am not familiar with such a need so I don't have a strong opinion. However, if an object needs to be emp

Re: Reuse object memory?

2015-04-20 Thread Ali Çehreli via Digitalmars-d-learn
On 04/20/2015 02:44 PM, Namespace wrote: > Thank you. Do you mean this is worth a PR, to add this > functionality to Phobos? I am not familiar with such a need so I don't have a strong opinion. However, if an object needs to be emplaced on top of an existing one, I can imagine that the origina

Re: Reuse object memory?

2015-04-20 Thread Namespace via Digitalmars-d-learn
Thank you. Do you mean this is worth a PR, to add this functionality to Phobos? My current code looks like this: http://dpaste.dzfl.pl/19b78a600b6c

Re: Reuse object memory?

2015-04-20 Thread Ali Çehreli via Digitalmars-d-learn
On 04/20/2015 12:05 PM, Namespace wrote: > I'm sorry if I annoy you Not at all! :) Sorry for not responding earlier. >, but I would really like to know how you would > reuse already instantiated storage of an existing object. > > Example code: > > final class Foo { > uint id; > >

Re: Reuse object memory?

2015-04-20 Thread Namespace via Digitalmars-d-learn
On Sunday, 19 April 2015 at 21:17:18 UTC, Ali Çehreli wrote: On 04/19/2015 09:04 AM, Namespace wrote: > Is it somehow possible to reuse the memory of an object? Yes, when you cast a class variable to void*, you get the address of the object. > @nogc > T emplace(T, Args...)(ref T obj, auto re

Re: Reuse object memory?

2015-04-19 Thread Namespace via Digitalmars-d-learn
It seems that D has currently no direct support to reuse object memory. D should add a new-placement syntax: Foo f = new Foo(42); new (f) Foo(23); and/or should add an emplace overload which takes an object: T emplace(T, Args...)(ref T obj, auto ref Args args) if (is(T == class

Re: Reuse object memory?

2015-04-19 Thread Namespace via Digitalmars-d-learn
And if I have an already instantiated object? Foo f = new Foo(); // reuse f's memory Is there an nicer way to override the memory instead of: void[] buf = (cast(void*) f)[0 .. __traits(classInstanceSize, Foo)]; buf = typeid(Foo).init[]; // or: buf = f.classinfo.init[]; ? The

Re: Reuse object memory?

2015-04-19 Thread Ali Çehreli via Digitalmars-d-learn
On 04/19/2015 09:04 AM, Namespace wrote: > Is it somehow possible to reuse the memory of an object? Yes, when you cast a class variable to void*, you get the address of the object. > @nogc > T emplace(T, Args...)(ref T obj, auto ref Args args) nothrow if (is(T == > class)) { There is already

Reuse object memory?

2015-04-19 Thread Namespace via Digitalmars-d-learn
Is it somehow possible to reuse the memory of an object? My current idea is: @nogc T emplace(T, Args...)(ref T obj, auto ref Args args) nothrow if (is(T == class)) { if (obj is null) return null; enum size_t SIZE = __traits(classInstanceSize, T); void[] buf = (cast(vo