automatic type creation

Frustrated c1514843 at drdrb.com
Mon Jan 27 05:21:50 PST 2014


Here is something I just did that essentially demonstrates what I
am talking about(bugs withstanding):

http://dpaste.dzfl.pl/d82b6274

It is not exactly how I should be going about it as it is
somewhat generic but not properly designed to allow the concrete
implementations needed

void main()
{
	void *data = null;
	auto a = A.New();
	auto b = A.New(otherA.StaticFactoryObjectData.Default);
	a.foo();
	b.foo();
}


Notice that myA and otherA are never referenced in the code
except possibly in the argument to New, which would generally be
loaded up from a file so it is irrelevant here.

So, the point is, basically, one could create a new subtype of A
and plug it in at runtime with the following two cases:

1. I never how to worry about what the new subtype might do or
even think about it much beyond using the static factory stuff,
which for the most part is taken care of in the
templates(slightly more work but not much)).

2. I do not have to change the code in any way from what it would
be without using the pattern(no plug and play) except that New
must keep tract of state data, which isn't much work beyond
saving and restoring the data to a file.

I simply use A, the interface, as the type and do what I want to
do with it and completely neglect any implementation of it(of
course I have to design a default myself but in some sense that
is irrelevant to the design since it is implementation based).


Using New() is bad because it prevents plug and play(the default
will always be used). But using New(objectData) is generic,
allows one to plug in any subtype of A as long as objectData is
not hard coded(basically it needs to be backed by a file and a
way for the user to modify the file(indirectly through a gui).

So the code essentially does this but isn't that useful as it
stands(using the templates makes it slightly difficult to extend
A properly... it can be done but I want even more generic way to
do this(right now initializing the object's with the data is the
issue. Serializing the subtype would probably make it generic
enough and wouldn't require much work from the user beyond using
the templates and the slight change in coding make New generic).


Anyways, maybe someone has some useful comments about this
approach(ways to make it better or pitfalls, etc...).


More information about the Digitalmars-d-learn mailing list