From: gotoken@... (GOTO Kentaro) Date: 1999-07-20T17:23:41+09:00 Subject: [ruby-talk:00508] Re: One question about classes written in C. Hi Cle, In message "[ruby-talk:00506] Re: One question about classes written in C." on 99/07/19, Yukihiro Matsumoto writes: >>Hmm, I think using is-a too much is bad funciton; use polymorphism >>instead, if possible. I think I'm not going to add it not to >>encourage using IS_A. > >In my class I need that function in the constructor and in the `+' >and `-' method. In the constructor I have to ensure that only >`Numeric's are used to initialize my instance. In the both arithmetic >methods, I have to know, whether they operate on an instance of that >class or not! Because adding a point to a point is a little bit >different from adding a scalar to a point. > >Is there another function to detect these? I often use `coerce technic' for the default action: switch (TYPE(y)) { case T_FIXNUM: case T_BIGNUM: /* something to do ... */ break; case T_FLOAT: /* something to do ... */ break; default: return rb_num_coerce_bin(x, y); } It's an idiomatic phrase. Indeed, we have only the three concrete classes as the numeric. # I'll add SingleFloat and DoubleFloat in the near future. But you can use IS_A() macro if you need. I would NOT like to stop that :-) >One would have doing that connection in the `initialize' function >again and again. So it would be no 1-n relation. One instance variable >- --> one memory location under C. Somthing like: > > rb_connect_var(rb_intern("@x"), point->x); > >Perhaps we could also going the way to allow to tie a variable >together with a method. Like your functions > > rb_define_hooked_variable, > rb_define_virtual_variable. > >But for instance variables also. > >Perhaps it could also be nice to have that feature on Ruby level too. I don't think it's nice because one doesn't have to set instance variables in order to customize the class but she can use private methods. From the other point of view, customizing by instance variables seems easy to cause bugs which are not easy to be found if the class is implemented in C, i.e. in the situation where one can't see how an instance variable is used inside of the object. -- gotoken # It is holiday `the sea day' in Japan but I don't know the meaning of # today..