View mode: basic / threaded / horizontal-split · Log in · Help
March 05, 2012
D RTTI?
I know D doesn't really have RTTI yet, but I'm experimenting with
"faking" it by doing something like:

	class A {
		string prop1;
		int prop2;
		...
		void serialize() {
			__serialize(this);
		}
	}

	void __serialize(T)(T obj) {
		writeln(typeid(obj));
		foreach (name; __traits(derivedMembers, T)) {
			writefln("%s = %s", name,
				__traits(getMember,obj,name));
		}
	}

The only thing is, serialize() has to be declared in every derived
class, because T needs to be known at compile-time. Is there a way to
"automate" this? I.e., automatically insert the serialize() boilerplate
code into derived classes?

(P.S.  D just took on brand new levels of cool when I realized I could
do something like this. Imagine doing this with C++ templates...  ugh!
What a painful thought!)


T

-- 
Tech-savvy: euphemism for nerdy.
March 05, 2012
Re: D RTTI?
On Mon, 05 Mar 2012 12:16:14 -0800, H. S. Teoh wrote:

> I know D doesn't really have RTTI yet, but I'm experimenting with
> "faking" it by doing something like:
> 
> 	class A {
> 		string prop1;
> 		int prop2;
> 		...
> 		void serialize() {
> 			__serialize(this);
> 		}
> 	}
> 
> 	void __serialize(T)(T obj) {
> 		writeln(typeid(obj));
> 		foreach (name; __traits(derivedMembers, T)) {
> 			writefln("%s = %s", name,
> 				__traits(getMember,obj,name));
> 		}
> 	}
> 
> The only thing is, serialize() has to be declared in every derived
> class, because T needs to be known at compile-time. Is there a way to
> "automate" this? I.e., automatically insert the serialize() boilerplate
> code into derived classes?
> 
> (P.S.  D just took on brand new levels of cool when I realized I could
> do something like this. Imagine doing this with C++ templates...  ugh!
> What a painful thought!)
> 
> 
> T

The normal approach is to use a string mixin statement in each derived 
class:

template Serializable()
{
	enum Serializable = q{...your code here...};
}

class B : A
{
  mixin(Serializable);
}

Unfortunately, I don't believe there's any mechanism to order derived 
classes to automatically perform the mixin.
March 06, 2012
Re: D RTTI?
On 2012-03-05 21:16, H. S. Teoh wrote:
> I know D doesn't really have RTTI yet, but I'm experimenting with
> "faking" it by doing something like:
>
> 	class A {
> 		string prop1;
> 		int prop2;
> 		...
> 		void serialize() {
> 			__serialize(this);
> 		}
> 	}
>
> 	void __serialize(T)(T obj) {
> 		writeln(typeid(obj));
> 		foreach (name; __traits(derivedMembers, T)) {
> 			writefln("%s = %s", name,
> 				__traits(getMember,obj,name));
> 		}
> 	}
>
> The only thing is, serialize() has to be declared in every derived
> class, because T needs to be known at compile-time. Is there a way to
> "automate" this? I.e., automatically insert the serialize() boilerplate
> code into derived classes?
>
> (P.S.  D just took on brand new levels of cool when I realized I could
> do something like this. Imagine doing this with C++ templates...  ugh!
> What a painful thought!)

If you actually want serialization, and this was not just an example, 
you can use Orange:

https://github.com/jacob-carlborg/orange
http://www.dsource.org/projects/orange/

-- 
/Jacob Carlborg
Top | Discussion index | About this forum | D home