www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - question about AutoImplement_Helper (and a couple of others)

reply Read Bixby <rbixby hotmail.com> writes:
I'm a bit new with the D programming language, so I figured this
would be the right place to ask a few questions that have been
piling up.

So let's start.  First, I recently found the AutoImplement class
while I was trying to build a Proxy object.  It seemed like an
interesting thing to try, though it may not be a good way to manage
asynchronously created resources (which is what I was planning to
use it for).

At any rate, since AutoImplement is a class already, and I have this
"thing" about not deriving from concrete classes, I decided use
AutoImplement_Helper instead.  I needed a proxied object, after all,
so I needed to be able to manipulate the class definition.  It was
only after doing so that I noticed it was marked private, and thus
presumably not intended for public consumption (and I have no idea
why it even compiles).

private shared class Proxy(InterfaceType) if (is (InterfaceType ==
interface)) : public InterfaceType
{
	private alias AutoImplement_Helper!("autoImplement_helper_",
"InterfaceType", InterfaceType, GeneratePassthroughMethod,
isAbstractFunction) autoImplement_helper_;
	public mixin(autoImplement_helper_.code);

	public shared static this()
	{
		s_proxy = new BlackHole!(InterfaceType);
	}

	public this()
	{
		m_instance = s_proxy;
	}

	public void setProxiedInstance(shared(InterfaceType)
instance)
	{
		m_instance = instance;
	}

	private static shared(InterfaceType) s_proxy;

	private InterfaceType m_instance;
}

private template GeneratePassthroughMethod(InterfaceType, method...)
{
	public const(string) GeneratePassthroughMethod =
"__traits(getMember, this.m_instance, __traits(identifier, self))
(args);";
}

Is there another way (that I just haven't seen) to do what I'm
trying to do?  I could just bite the bullet and derive my proxy from
AutoImplement, but I thought I'd ask first.

Next, you may have noticed that it's a shared class.  I wanted to
make sure that the assignment I'm doing in the setProxiedInstance()
method will be atomic, and that reading the variable will also be
atomic.

My third question is about attributes.  As far as I can tell, D has
no user defined attributes, correct?  I was making myself a unit
test framework (with simple reporting, encapsulation of unit tests
as methods, and assertion tools).  I was hoping to perform automatic
registration of the individual unit tests, but the best I could
manage was compile-time detection of methods starting with "test",
and this feels like a hack.  I would prefer to mark the methods
explicitly in some way.  Does anyone know of a way to do this?

Thanks for your time.
Apr 15 2011
parent bearophile <bearophileHUGS lycos.com> writes:
Read Bixby:

 (and I have no idea why it even compiles).
If it's in the same module then it compiles because everything in a module is public to each other. If it's in another module then it's a compiler bug.
 My third question is about attributes.  As far as I can tell, D has
 no user defined attributes, correct?
Right. But I think they will be added someday. We have discussed this many times, despite I think there is no concrete proposal yet.
 I was making myself a unit
 test framework (with simple reporting, encapsulation of unit tests
 as methods, and assertion tools).  I was hoping to perform automatic
 registration of the individual unit tests, but the best I could
 manage was compile-time detection of methods starting with "test",
 and this feels like a hack.  I would prefer to mark the methods
 explicitly in some way.  Does anyone know of a way to do this?
A unit test module will be soon added to Phobos. Currently the unittest hooks and D introspection capabilities aren't so strong. More people need to ask for this basic capabilities in the main D newsgroup to eventually let the D devs know that here there is a need. Bye, bearophile
Apr 15 2011