www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Dynamic polymorphism

reply macky <martin.butina gmail.com> writes:
Hi all!

I'm trying to figure out how dynamic polymorhism is done the right way in D...

interface I
{
    ...some required functions...
}

class BaseClass()
{
    ...implementation of interface...
}

class Customer() : BaseClass
{
   ...some members and overriden baseclass functions...
}

class Process()
{
    void DoSomething(BaseClass object)
    {
        //I want to process all classes derived from base class
    }
}

void Main()
{
   Customer customer = new Customer();
   Process process = new Process();
   process.DoSomething(customer);
}

Even though this is working tupleof is not returning any value (I suppose
becouse when creating an instance customer is casted in base class and the link
is broken - actualy we are dealing with a partial clone).
Is this the right way of doing things? I know that in c++ you should provide a

provided by reference. I tried to provide a "ref" in the input params ( void
DoSomething(ref BaseClass object)) but this is not accepted. How can I solve
this? Thanx.

br, Martin
Apr 12 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"macky" <martin.butina gmail.com> wrote in message 
news:evksq3$223e$1 digitalmars.com...
 Hi all!

 I'm trying to figure out how dynamic polymorhism is done the right way in 
 D...

 interface I
 {
    ...some required functions...
 }

 class BaseClass()
 {
    ...implementation of interface...
 }

 class Customer() : BaseClass
 {
   ...some members and overriden baseclass functions...
 }

 class Process()
 {
    void DoSomething(BaseClass object)
    {
        //I want to process all classes derived from base class
    }
 }

 void Main()
 {
   Customer customer = new Customer();
   Process process = new Process();
   process.DoSomething(customer);
 }

 Even though this is working tupleof is not returning any value (I suppose 
 becouse when creating an instance customer is casted in base class and the 
 link is broken - actualy we are dealing with a partial clone).
 Is this the right way of doing things? I know that in c++ you should 

 everything is provided by reference. I tried to provide a "ref" in the 
 input params ( void DoSomething(ref BaseClass object)) but this is not 
 accepted. How can I solve this? Thanx.
What _are_ you doing, anyway? Why do you need to use .tupleof to do polymorphism, why are all your classes templates with empty parameter lists, and what does "I want to process all classes derived from base class" mean?
Apr 12 2007
parent reply macky <martin.butina gmail.com> writes:
Jarrett Billingsley Wrote:

 "macky" <martin.butina gmail.com> wrote in message 
 news:evksq3$223e$1 digitalmars.com...
 Hi all!

 I'm trying to figure out how dynamic polymorhism is done the right way in 
 D...

 interface I
 {
    ...some required functions...
 }

 class BaseClass()
 {
    ...implementation of interface...
 }

 class Customer() : BaseClass
 {
   ...some members and overriden baseclass functions...
 }

 class Process()
 {
    void DoSomething(BaseClass object)
    {
        //I want to process all classes derived from base class
    }
 }

 void Main()
 {
   Customer customer = new Customer();
   Process process = new Process();
   process.DoSomething(customer);
 }

 Even though this is working tupleof is not returning any value (I suppose 
 becouse when creating an instance customer is casted in base class and the 
 link is broken - actualy we are dealing with a partial clone).
 Is this the right way of doing things? I know that in c++ you should 

 everything is provided by reference. I tried to provide a "ref" in the 
 input params ( void DoSomething(ref BaseClass object)) but this is not 
 accepted. How can I solve this? Thanx.
What _are_ you doing, anyway? Why do you need to use .tupleof to do polymorphism, why are all your classes templates with empty parameter lists, and what does "I want to process all classes derived from base class" mean?
ah. sorry. Classes are empty due to my lazyness ;). Actualy there are some virtual methods defined in a base class but some members are are defined just in a derived class. My idea was that I would get this members with tuppleof function (I guess I'm trying to fake the reflection). These members do not exist in a base class ofcourse but my question is if this is possible the way I wanted to perform this task... Since my DoSomething() function is expecting the base class and in implementation I have provided the derived class I would expect that it is handled as a derived class. But I realize that this derived class is cast in a base class and therefor no member is returned. I would like to avoid this but I don't know how. I thought that I'm not using input parameters correctly? regards, Martin
Apr 13 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"macky" <martin.butina gmail.com> wrote in message 
news:evnbo3$30r2$1 digitalmars.com...
 ah. sorry. Classes are empty due to my lazyness ;). Actualy there are some 
 virtual methods defined in a base class but some members are are defined 
 just in a derived class. My idea was that I would get this members with 
 tuppleof function (I guess I'm trying to fake the reflection). These 
 members do not exist in a base class ofcourse but my question is if this 
 is possible the way I wanted to perform this task... Since my 
 DoSomething() function is expecting the base class and in implementation I 
 have provided the derived class I would expect that it is handled as a 
 derived class. But I realize that this derived class is cast in a base 
 class and therefor no member is returned. I would like to avoid this but I 
 don't know how. I thought that I'm not using input parameters correctly?

 regards, Martin
Ah, I see now. Depending on what you're trying to do, tupleof might be unnecessary. You could design it the other way around, like so: class BaseClass() { ...implementation of interface... // To be overridden void processMe(Process p); } class Customer() : BaseClass { ...some members and overriden baseclass functions... void processMe(Process p) { // do the processing } } class Process() { void DoSomething(BaseClass object) { // this will call the appropriate process method based // on the derived class type object.process(this); } } But that you want to access data fields of classes and not just the methods makes me think you want to do something more complex, like .. serializing the members of a class to a file? I'm wondering why you want to access to the data fields of the class.
Apr 13 2007
parent reply macky <martin.butina gmail.com> writes:
You're right! Serialization is just one of the benefit comming from this. But
I'm planing to do something even more interesting. I'm trying to port my DAL
(data access layer) into d and share it with d community. I was planning to
bind it to ddbi. From my experiances this is the most convinient way of
handling data in a bussiness application (transaction pattern). Unfortunately
I'm having a hard time accessing fields dynamicaly. I also miss some meta

I embeded tupleof in a class itself (similar to your suggestion) it does not
return it's own fields. *doh*. I'm a bit of stuck here. In order to do what I
have planned I to access fields somehow.

cheers, martin


Jarrett Billingsley Wrote:

 "macky" <martin.butina gmail.com> wrote in message 
 news:evnbo3$30r2$1 digitalmars.com...
 ah. sorry. Classes are empty due to my lazyness ;). Actualy there are some 
 virtual methods defined in a base class but some members are are defined 
 just in a derived class. My idea was that I would get this members with 
 tuppleof function (I guess I'm trying to fake the reflection). These 
 members do not exist in a base class ofcourse but my question is if this 
 is possible the way I wanted to perform this task... Since my 
 DoSomething() function is expecting the base class and in implementation I 
 have provided the derived class I would expect that it is handled as a 
 derived class. But I realize that this derived class is cast in a base 
 class and therefor no member is returned. I would like to avoid this but I 
 don't know how. I thought that I'm not using input parameters correctly?

 regards, Martin
Ah, I see now. Depending on what you're trying to do, tupleof might be unnecessary. You could design it the other way around, like so: class BaseClass() { ...implementation of interface... // To be overridden void processMe(Process p); } class Customer() : BaseClass { ...some members and overriden baseclass functions... void processMe(Process p) { // do the processing } } class Process() { void DoSomething(BaseClass object) { // this will call the appropriate process method based // on the derived class type object.process(this); } } But that you want to access data fields of classes and not just the methods makes me think you want to do something more complex, like .. serializing the members of a class to a file? I'm wondering why you want to access to the data fields of the class.
Apr 15 2007
parent macky <martin.butina ice.si> writes:
I found a solution, though I'm not sure why things behave like this...
It works if a derived class has its own implementation of GetType(int index) or
GetValue(int index) and base class has only abstract methods of the two. I
would expect if I implement methods in a base class all children would also
implement this method. But it seems that the child class needs to override it
to do the job... yep, tupleof works in a misterious ways ;)


macky Wrote:

 You're right! Serialization is just one of the benefit comming from this. But
I'm planing to do something even more interesting. I'm trying to port my DAL
(data access layer) into d and share it with d community. I was planning to
bind it to ddbi. From my experiances this is the most convinient way of
handling data in a bussiness application (transaction pattern). Unfortunately
I'm having a hard time accessing fields dynamicaly. I also miss some meta

I embeded tupleof in a class itself (similar to your suggestion) it does not
return it's own fields. *doh*. I'm a bit of stuck here. In order to do what I
have planned I to access fields somehow.
 
 cheers, martin
 
 
 Jarrett Billingsley Wrote:
 
 "macky" <martin.butina gmail.com> wrote in message 
 news:evnbo3$30r2$1 digitalmars.com...
 ah. sorry. Classes are empty due to my lazyness ;). Actualy there are some 
 virtual methods defined in a base class but some members are are defined 
 just in a derived class. My idea was that I would get this members with 
 tuppleof function (I guess I'm trying to fake the reflection). These 
 members do not exist in a base class ofcourse but my question is if this 
 is possible the way I wanted to perform this task... Since my 
 DoSomething() function is expecting the base class and in implementation I 
 have provided the derived class I would expect that it is handled as a 
 derived class. But I realize that this derived class is cast in a base 
 class and therefor no member is returned. I would like to avoid this but I 
 don't know how. I thought that I'm not using input parameters correctly?

 regards, Martin
Ah, I see now. Depending on what you're trying to do, tupleof might be unnecessary. You could design it the other way around, like so: class BaseClass() { ...implementation of interface... // To be overridden void processMe(Process p); } class Customer() : BaseClass { ...some members and overriden baseclass functions... void processMe(Process p) { // do the processing } } class Process() { void DoSomething(BaseClass object) { // this will call the appropriate process method based // on the derived class type object.process(this); } } But that you want to access data fields of classes and not just the methods makes me think you want to do something more complex, like .. serializing the members of a class to a file? I'm wondering why you want to access to the data fields of the class.
Apr 16 2007