slang-users mailing list

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

Re: [slang-users] Can you do OO in Slang?


Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx> wrote:
> Ah, OK, that's fine. But I have to ask: is typecasting of truck onto a car
> not possible? I.e.: you cannot typecast() complex types (structs)? It seems
> to me that typecast() operates on and returns values, while it should/could
> operate on pointers/references if such typecasting of structs would to have
> sense. Am I right? I'm asking because I've used to such inheritance
> emulation in C, where one has:
>
> typedef struct Button_s {
>     Widget base;  /* simulated C++-like inheritance of Widget struct */
>     int is_pressed;
>} Button;
>
> to then be able to do:
>
> my_base_widget_function ((Widget *)&button);
>
> It would be interesting to do the same in SLang.

Such typecasting is not necessary for a dynamically typed language.
Consider:

   $ cat foo.sl
   variable
     Widget_Type = struct { generic = 1, my_type = "Widget",};
     Button_Type = struct { @Widget_Type, is_pressed = 0, my_type = "Button"};

   private define my_base_widget_function (w)
   {
      vmessage ("generic = %d, my_type = %s", w.generic, w.my_type);
   }
   define slsh_main ()
   {
      variable b = @Button_Type;
      my_base_widget_function (b);
   }

   $ slsh foo.sl
   generic = 1, my_type = Button

--John

>
> On Wed, 27 Jan 2021 at 23:15, John E. Davis <jed@xxxxxxxxxxx> wrote:
>
>> Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx> wrote:
>> > in one of the posts that announced a new libslang version I've noticed a
>> > syntax that seems to be an inheritance, i.e.:
>> >
>> >    car = struct { name, price, max_velocity}
>> >    truck = struct {@car, max_load};
>> >
>> > Now truck includes all the fields that car has. I wonder however if you
>> can
>> > cast a truck at a car, to pass it to a generic car-related function? Is
>> > there casting in slang?
>>
>> Yes, you can pass truck to a routine that expects a car as long as all
>> of the fields that the car function needs are present ("duck typing").
>> The SLxfig module <https://www.jedsoft.org/fun/slxfig/> makes
>> extensive use of this approach.  --John
>> _______________________________________________
>> For list information, visit <http://jedsoft.org/slang/mailinglists.html>.
>>
>>
>
> -- 
> Sebastian Gniazdowski
_______________________________________________
For list information, visit <http://jedsoft.org/slang/mailinglists.html>.


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