Re: Reset all Members of a Aggregate Instance

2015-12-08 Thread Marc Schütz via Digitalmars-d-learn
On Tuesday, 8 December 2015 at 05:13:51 UTC, Chris Wright wrote: On Tue, 08 Dec 2015 14:12:02 +1100, Daniel Murphy wrote: On 4/12/2015 8:38 AM, Chris Wright wrote: An object reference is just a pointer, but we can't directly cast it. So we make a pointer to it and cast that; the type system

Re: Reset all Members of a Aggregate Instance

2015-12-07 Thread Daniel Murphy via Digitalmars-d-learn
On 4/12/2015 8:38 AM, Chris Wright wrote: An object reference is just a pointer, but we can't directly cast it. So we make a pointer to it and cast that; the type system allows it. Now we can access the data that the object reference refers to directly. Casting is fine too: cast(void*)classRef

Re: Reset all Members of a Aggregate Instance

2015-12-07 Thread Chris Wright via Digitalmars-d-learn
On Tue, 08 Dec 2015 14:12:02 +1100, Daniel Murphy wrote: > On 4/12/2015 8:38 AM, Chris Wright wrote: >> An object reference is just a pointer, but we can't directly cast it. >> So we make a pointer to it and cast that; the type system allows it. >> Now we can access the data that the object

Re: Reset all Members of a Aggregate Instance

2015-12-05 Thread Ali Çehreli via Digitalmars-d-learn
On 12/05/2015 01:32 AM, Observer wrote: Won't clear(c); do the trick? ((pp187-188 of TDPL) clear() has been renamed as destroy() but it won't work by itself because the OP wants a reusable object. I think, in addition to destroy(), the default constructor should be run:

Re: Reset all Members of a Aggregate Instance

2015-12-05 Thread Chris Wright via Digitalmars-d-learn
On Sat, 05 Dec 2015 07:48:16 -0800, Ali Çehreli wrote: > On 12/05/2015 01:32 AM, Observer wrote: > >> Won't clear(c); do the trick? ((pp187-188 of TDPL) > > clear() has been renamed as destroy() but it won't work by itself > because the OP wants a reusable object. I think, in addition to >

Re: Reset all Members of a Aggregate Instance

2015-12-05 Thread Jakob Ovrum via Digitalmars-d-learn
On Saturday, 5 December 2015 at 16:28:18 UTC, Chris Wright wrote: The default constructor doesn't set default field values, though, which is why my solution involved copying ClassInfo.init. Thanks, this is a handy factoid. Reminds me of the whole __dtor vs __xdtor debacle.

Re: Reset all Members of a Aggregate Instance

2015-12-05 Thread Observer via Digitalmars-d-learn
On Thursday, 3 December 2015 at 21:04:00 UTC, Nordlöw wrote: Given class C { // lots of members } and a function f(C c) { } is there a generic way, perhaps through reflection, to reset (inside f) all members of `c` to their default values? Something along

Re: Reset all Members of a Aggregate Instance

2015-12-04 Thread Enamex via Digitalmars-d-learn
On Thursday, 3 December 2015 at 21:04:00 UTC, Nordlöw wrote: ... Unless I'm missing something very important: Isn't that essentially what the `out` attribute on a function parameter does?

Re: Reset all Members of a Aggregate Instance

2015-12-03 Thread Tofu Ninja via Digitalmars-d-learn
On Thursday, 3 December 2015 at 21:04:00 UTC, Nordlöw wrote: ... I think reflection will be a bad choice for this because of private members and what not. I think the correct way is: void reset(C)(ref C c) { static if(is(C == class)) { auto init =

Re: Reset all Members of a Aggregate Instance

2015-12-03 Thread Tofu Ninja via Digitalmars-d-learn
On Friday, 4 December 2015 at 04:08:33 UTC, Tofu Ninja wrote: On Thursday, 3 December 2015 at 21:04:00 UTC, Nordlöw wrote: ... I think reflection will be a bad choice for this because of private members and what not. I think the correct way is: void reset(C)(ref C c) { static

Re: Reset all Members of a Aggregate Instance

2015-12-03 Thread Sebastiaan Koppe via Digitalmars-d-learn
On Thursday, 3 December 2015 at 21:13:59 UTC, Nordlöw wrote: Need to assert that not a function and mutability (std.traits.isMutable) Yeah you need to do that.

Re: Reset all Members of a Aggregate Instance

2015-12-03 Thread Jakob Ovrum via Digitalmars-d-learn
On Thursday, 3 December 2015 at 21:04:00 UTC, Nordlöw wrote: Given class C { // lots of members } and a function f(C c) { } is there a generic way, perhaps through reflection, to reset (inside f) all members of `c` to their default values? Something along

Re: Reset all Members of a Aggregate Instance

2015-12-03 Thread Nordlöw via Digitalmars-d-learn
On Thursday, 3 December 2015 at 21:04:00 UTC, Nordlöw wrote: Something along foreach(ref member; __traits(allMembers, c)) { member = typeof(member).init; } This works for me: void resetAllMembers(T)(T c) if (is(T == class)) { foreach (ref m; c.tupleof) {

Re: Reset all Members of a Aggregate Instance

2015-12-03 Thread Nordlöw via Digitalmars-d-learn
On Thursday, 3 December 2015 at 21:38:48 UTC, Chris Wright wrote: The terrible way is something like: void reset(Object o) in { assert(!(o is null)); } body { auto p = cast(ubyte*)*cast(void**) auto ci = o.classinfo; auto init = cast(ubyte)ci.init; p[0..init.length] = init[]; if

Re: Reset all Members of a Aggregate Instance

2015-12-03 Thread Chris Wright via Digitalmars-d-learn
On Thu, 03 Dec 2015 21:55:04 +, Nordlöw wrote: > On Thursday, 3 December 2015 at 21:38:48 UTC, Chris Wright wrote: >> The terrible way is something like: >> >> void reset(Object o) >> in { >> assert(!(o is null)); >> } >> body { >> auto p = cast(ubyte*)*cast(void**) >> auto ci =

Reset all Members of a Aggregate Instance

2015-12-03 Thread Nordlöw via Digitalmars-d-learn
Given class C { // lots of members } and a function f(C c) { } is there a generic way, perhaps through reflection, to reset (inside f) all members of `c` to their default values? Something along foreach(ref member; __traits(allMembers, c)) {

Re: Reset all Members of a Aggregate Instance

2015-12-03 Thread Adam D. Ruppe via Digitalmars-d-learn
On Thursday, 3 December 2015 at 21:04:00 UTC, Nordlöw wrote: is there a generic way, perhaps through reflection, to reset (inside f) all members of `c` to their default values? You could always copy the init back over it. For a struct: s = Struct.init; for a class... well, the code is a lot

Re: Reset all Members of a Aggregate Instance

2015-12-03 Thread Nordlöw via Digitalmars-d-learn
On Thursday, 3 December 2015 at 21:04:00 UTC, Nordlöw wrote: Given class C { // lots of members } and a function f(C c) { } is there a generic way, perhaps through reflection, to reset (inside f) all members of `c` to their default values? Something along

Re: Reset all Members of a Aggregate Instance

2015-12-03 Thread Sebastiaan Koppe via Digitalmars-d-learn
Haven't compiled but it should look something like this: foreach(member; __traits(allMembers, typeof(c))) __traits(getMember, c, member) = typeof(__traits(getMember, c, member)).init;

Re: Reset all Members of a Aggregate Instance

2015-12-03 Thread Nordlöw via Digitalmars-d-learn
On Thursday, 3 December 2015 at 21:08:30 UTC, Sebastiaan Koppe wrote: Haven't compiled but it should look something like this: foreach(member; __traits(allMembers, typeof(c))) __traits(getMember, c, member) = typeof(__traits(getMember, c, member)).init; Need to assert that not a function

Re: Reset all Members of a Aggregate Instance

2015-12-03 Thread Chris Wright via Digitalmars-d-learn
The terrible way is something like: void reset(Object o) in { assert(!(o is null)); } body { auto p = cast(ubyte*)*cast(void**) auto ci = o.classinfo; auto init = cast(ubyte)ci.init; p[0..init.length] = init[]; if (ci.defaultConstructor) { ci.defaultConstructor(o); } else {