slang-users mailing list

[2006 Date Index] [2006 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: [slang-users] SLang with objects ?


Marko Mahnic <marko.mahnic@xxxxxxxx> 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

_______________________________________________
To unsubscribe, visit http://jedsoft.org/slang/mailinglists.html


[2006 date index] [2006 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]