Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #1261 (new enhancement)

Opened 16 years ago

Last modified 14 years ago

Add a method to get property names of properties of classes / unions / structs

Reported by: bmeck Assigned to: community
Priority: normal Milestone: 2.0
Component: Core Functionality Version: 0.99.7 Dominik
Keywords: Cc:

Description

Sometimes it is useful when creating documentation or interfaces and meta-programming in general to inspect an object for it's property names. As such here is a home grown template to do so.

import tango.io.Stdout;

template getProperties(alias t,bool getBase = true,uint i = 0)
{
	static if(getBase && is(t x == super))
	{
		static if(t.tupleof.length != 0)
		{
			static if(x[0].tupleof.length > 0)
				const char[][] getProperties = getProperties!(t,false,0)~getProperties!(x[0],false,0);
			else
				const char[][] getProperties = getProperties!(t,false,0);
		}
		else
			const char[][] getProperties = getProperties!(x[0],false,0);
	}
	else static if(t.tupleof.length > i)
		const char[][] getProperties = ([t.tupleof[i].stringof]~getProperties!(t,false,i+1));
	else
		const char[][] getProperties = [];
}

struct Fizz
{
	uint i;
}

class Bar
{
	int bar;
}

class Foo : Bar{
	int foo;
	int foo2;
}

class Foobar : Foo{}

union Boo{
	int i;
	uint k;
}

void main() {
	Foo f;
	Stdout(getProperties!(typeof(f))).newline;
	Stdout(getProperties!(typeof(cast(Bar)f))).newline;
	Stdout(getProperties!(Boo)).newline;
	Stdout(mixin("\""~getProperties!(Fizz)[0]~"\"")).newline;
	Stdout(mixin("\""~getProperties!(Foo)[1]~"\"")).newline;
	Stdout(mixin("\""~getProperties!(Foo,false)[1]~"\"")).newline;
	Stdout(mixin("\""~getProperties!(Foobar)[1]~"\"")).newline;
}

Change History

08/22/08 20:28:30 changed by bmeck

  • type changed from defect to enhancement.

(follow-up: ↓ 4 ) 09/03/08 12:16:47 changed by Cyborg16

Comment: I'd agree that this is useful in general, but can such a generic function be useful for meta-programming? Probably actually a group of similar small templates could be useful. (Actually I'm quite impressed with your function. Is it possible to get just the property name without the struct/class/union name? I'll have a play later...)

09/08/08 20:50:08 changed by sean

  • milestone changed from 0.99.8 to 1.0.

(in reply to: ↑ 2 ) 09/10/08 18:08:02 changed by bmeck

Replying to Cyborg16:

Comment: I'd agree that this is useful in general, but can such a generic function be useful for meta-programming? Probably actually a group of similar small templates could be useful. (Actually I'm quite impressed with your function. Is it possible to get just the property name without the struct/class/union name? I'll have a play later...)

Yes you can extrapolate the singular names at CTFE but also I liked that I could include the source of where a property came from using this method but perhaps splitting it into more parts would be wise

11/09/09 01:15:11 changed by kris

  • owner changed from sean to fawzi.

11/29/09 13:12:51 changed by fawzi

  • owner changed from fawzi to kris.

04/24/10 22:24:06 changed by kris

  • owner changed from kris to community.
  • milestone changed from 1.0 to 2.0.