Interface question

Reiner Pope reiner.pope at REMOVE.THIS.gmail.com
Thu Aug 31 15:52:15 PDT 2006


Charles D Hixson wrote:
> David Medlock wrote:
>> Charles D Hixson wrote:
>>> xs0 wrote:
>>>
>>>> ...Ignore this if I am misreading you - David)
>>
>> I don't quite 'get' what you are trying to accomplish(not technically 
>> but in a design sense).
>>
>> Unless you are attempting to add compiled classes at runtime, I would 
>> be surprised if D could not accomplish what you are doing.
>>
>> To create an object which has a known constructor, use a template 
>> function/object....but there is a better way.
>>
>> Have your objects created with no argument constructors then 
>> configured using a common method.  I have a text format(XML lite) I 
>> use in which each node is a 'constructor' for an object.  Then the 
>> children of the node are passed to the config() method of the object.  
>> In this way I can have many similar objects which are combined in 
>> different ways.
>>
>> Is this close to what you are trying?
>> -David
> The problem is, when the object is being read in from the file, the type 
> of the object (on the file) determines the desired type of the created 
> object.  This appears to require the kind of run-time parsing that 
> Python, Smalltalk, and Ruby have...and that D doesn't.  I though I'd 
> find away around it, but the only way I found involved storing 
> everything in a common format (very inefficient) and making all the 
> calls through multiple layers of indirection.  If I do all THAT I might 
> as well use Python, which at least doesn't fight me every step of the way.
I don't understand quite what the difficulty is. If I understand you, 
you are deserializing objects, but you don't know what type they are. 
After you have deserialized them, you presumably want to do something 
with them. Am I right?

So, presuming you want to do something with them, you can probably 
expect that all the objects can do such a thing. So we declare an interface:

interface ObjectIveDeserialized
{
     void doTheThingIWantThemToDo();
}

Then, presuming that you will know all of the formats they can be in at 
compile-time (you must, else how could you write a parser for them?) 
then you just declare another interface:

interface FileParser
{
     ObjectIveDeserialized 
tryToDeserializeThisAndReturnNullIfIFail(Stream input);
}

Then you just have a list of all FileParsers and try each of them until 
one works.

Maybe I'm completely misunderstanding you, but this doesn't seem to bad 
in D. How would you do it in a dynamic language like Python, Smalltalk, 
Ruby?

Cheers,

Reiner



More information about the Digitalmars-d-learn mailing list