[slang-users] SLang with objects ?
John E. Davis
davis at space.mit.edu
Mon Oct 16 06:42:45 EDT 2006
Marko Mahnic <marko.mahnic at email.si> wrote:
>The thing that I am missing the most is a way to add new members
>to an existing structure. This way I could add some polymorphism.
>I found a way to do it (with an empty extra field in the base "class")
>but I don't consider this to be a solution:
I use code such as this:
require ("structfuns");
private define default_print(obj) { ...}
private define default_rotate(obj) { ...}
private define default_translate(obj) { ...}
private variable Base_Type = struct
{
print, rotate, translate
};
Base_Type.print = &default_print;
Base_Type.print = &default_rotate;
Base_Type.print = &default_translate;
define derived_type (fields)
{
variable type = @Base_Type;
return struct_combine (m, fields);
}
private define foo_method (foo)
{
if (foo.x == 3) do_something();
something_else ();
}
private define foo_print (foo)
{
vmessage ("foo: x = %g", x);
}
define new_foo (x)
{
variable s = derived_type (["foo", "x"]);
s.method = &foo_method;
s.x = x;
return s;
}
foo = new_foo (3.1);
foo.rotate ();
foo.method ();
foo.print ();
--John
More information about the slang-users-l
mailing list