#include "cll1.h"
/* This is C<<1 rewrite of object polymorphism from
- http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming */
-
-def_type(Animal);
+ http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
+
+ Of course there are easier ways to do the same thing... :-)
+ (G)2008 xChas */
def_mem(Actions)
{
- void method(init) (Animal self, str name);
- str method(talk) (Animal self);
+ str method(talk) (anonymous self);
};
-def_obj(Animal)
+def_mem(Animal)
{
interface(Actions);
char *name;
};
-Animal construct(Animal) (Animal self, str name)
+construct(Animal) (Animal self, str name)
{
self->name = name;
return self;
}
-str cat_talk(Animal self)
+str cat_talk(anonymous self)
{
return "Meow!";
}
-str dog_talk(Animal self)
+str dog_talk(anonymous self)
{
return "Arf! Arf!";
}
Actions cat_actions(void)
{
- Actions this=get_mem(Actions);
+ Get_mem(this,Actions);
+
this->talk = cat_talk;
return this;
}
Actions dog_actions(void)
{
- Actions this=get_mem(Actions);
+ Get_mem(this,Actions);
+
this->talk = dog_talk;
return this;
}
Actions cat = cat_actions();
Actions dog = dog_actions();
- animal[0] = get_obj(Animal, cat, "Missy");
- animal[1] = get_obj(Animal, cat, "Mr. Bojangles");
- animal[2] = get_obj(Animal, dog, "Lassie");
+ animal[0] = get_obj(Animal,cat,"Missy");
+ animal[1] = get_obj(Animal,cat,"Mr. Bojangles");
+ animal[2] = get_obj(Animal,dog,"Lassie");
for_range(i, 0, 2)
{
- printf("%s: %s\n", animal[i]->name, _(talk, animal[i]));
+ print(animal[i]->name,":",_(talk,animal[i]));
}
}