Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

multiple dispatch

Name: Anonymous 2012-01-21 4:10

Lisp:


[1]> (defclass B () ())
#<STANDARD-CLASS B>
[2]> (defclass C () ())
#<STANDARD-CLASS C>
[3]> (defgeneric f (x y))
#<STANDARD-GENERIC-FUNCTION F>
[4]> (defmethod f ((x B) (y B)) (print "BB"))
#<STANDARD-METHOD (#<STANDARD-CLASS B> #<STANDARD-CLASS B>)>
[5]> (defmethod f ((x B) (y C)) (print "BC"))
#<STANDARD-METHOD (#<STANDARD-CLASS B> #<STANDARD-CLASS C>)>
[6]> (defmethod f ((x C) (y B)) (print "CB"))
#<STANDARD-METHOD (#<STANDARD-CLASS C> #<STANDARD-CLASS B>)>
[7]> (defmethod f ((x C) (y C)) (print "CC"))
#<STANDARD-METHOD (#<STANDARD-CLASS C> #<STANDARD-CLASS C>)>
[8]> (f (make-instance 'B) (make-instance 'B))

"BB"
"BB"
[9]> (f (make-instance 'B) (make-instance 'C))

"BC"
"BC"
[10]> (f (make-instance 'C) (make-instance 'B))

"CB"
"CB"
[11]> (f (make-instance 'C) (make-instance 'C))

"CC"
"CC"
[12]>
Bye.


C++:


#include <iostream>

using namespace std;

struct A;
struct B;
struct C;

struct A {
  virtual ~A() {}

  virtual void f(A* y) = 0;

  virtual void f(B* y) = 0;
  virtual void f(C* y) = 0;

  virtual void g(B* y) = 0;
  virtual void g(C* y) = 0;
};

struct B : public A {
  void f(A* y);

  void f(B* y);
  void f(C* y);

  void g(B* y);
  void g(C* y);
};

struct C : public A {
  void f(A* y);

  void f(B* y);
  void f(C* y);

  void g(B* y);
  void g(C* y);
};

void B::f(A* y) { y->g(this); }

void B::f(B* y) { cout << "BB" << endl; }
void B::f(C* y) { cout << "BC" << endl; }

void B::g(B* y) { y->f(this); }
void B::g(C* y) { y->f(this); }


void C::f(A* y) { y->g(this); }

void C::f(B* y) { cout << "CB" << endl; }
void C::f(C* y) { cout << "CC" << endl; }

void C::g(B* y) { y->f(this); }
void C::g(C* y) { y->f(this); }

int main(int argc, char** argv) {
  A* b = new B();
  A* c = new C();
  b->f(b);
  b->f(c);
  c->f(b);
  c->f(c);
  delete b;
  delete c;
  return 0;


$ g++ gen.cpp
$ a.out
BB
BC
CB
CC

Name: Anonymous 2012-01-21 4:13

>C++

ONE WORD: THE FORCED UGLIFICATION OF CODE

Name: Anonymous 2012-01-21 4:18

>>2

To be, fair, one can use RTI or something to get an id for the class of y, and do a switch statement off of it, and the code size ends up being around the same as the lisp version. I was just trying to implement multiple dispatch without using any switch statements.

Name: >>3 2012-01-21 4:20

is it called RTI? seeples has too many buzzwords/buzzernyms for me.

Name: Anonymous 2012-01-21 4:23

>>3
one can use RTI or something to get an id for the class of y, and do a switch statement off of it

ONE WORD: THE FORCED UGLIFICATION OF CODE

Name: Anonymous 2012-01-21 4:25

>>1
C11:
#include <stdio.h>

typedef struct B {;} B;
typedef struct C {;} C;

#define f(x,y) _Generic((x),struct B:_Generic((y),struct B:puts("BB"),struct C:puts("BC")),\
                            struct C:_Generic((y),struct B:puts("CB"),struct C:puts("CC")))

int main(void) {
    f((B){},(B){});
    f((B){},(C){});
    f((C){},(B){});
    f((C){},(C){});
}

Name: Anonymous 2012-01-21 4:26

>>5

hehe indeed.

Name: Anonymous 2012-01-21 4:30

>>6

is dat a generic macro? wow

Name: Anonymous 2012-01-21 4:30

>>6

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::/::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::://::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::/o++/+so/::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::s:`   `yy:::/::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::/h-`   `sd/:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::+yhyy+/:::::::::::+y+::/ydo::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::/::/hmhhd+::::::::::://+odmdh/::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::/:::://:/ymyyms/:::::::::::://dhyhy::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::://shddyyyyds:::/:::::///:odyyyhs/::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::://oydhyyyyyydy:/::::::::://yysysyh+:/::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::ymhyyyyyyyyhd+:/:::::::::+dyyssyyy/::/:::::://::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::dmyyyyyyyyyymo:/:::::/:::shsyysyyhsoooooossoooso+:/:::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::/sddyyyyyyyyyhms///:::://:/dysyyyhys+-:++::+oo/:://:/:::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::oNhyyyyyyyyyyyhhhs/::/:::/shyyyhy/.``-:.``.-.:so://:::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::oNhyyyyyyyyyyyyyydhys/::/+dyyhs:`   ``   :dNs`.yo::/::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::hmyyyyyyyyyyyyyyyyyyhdo//ydho-.://:`    `smMh  -d/::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::/yNyyyyyyyyyyyyyyyyyyyhdosy/.``oyyyy+`   -sdNo ``d+/++osso+/:/://////::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::/dmhyyyyyyyyyyyhhhyyyhdo-`   `+syyy+`   :mNy-+oohyyyyso+shho/::o+/os//:::::::/::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::omhyyyyyyyyyyyo:+dhhs:.   ``.-:/++:`  .:./+-ohyyyyyy+````/hdo:/s.``/y+++/++++++/::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::hmyyyyyyyyho:`` .s/.```-/sydmddhyssso/----..yyyyyyyy:````:ydy:o+s-  oy--------:+o+///:::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::sNhyyyyyyyh-`    ```.+ymmmmmds+/:::/+syy:```odyyyyyys/--/shdo:y/m+  .h+-``     ``-/+o+//:/::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::/ymdhhhhhhd+      -oo++osyhhhhys+/:-:::+yy-`.hdhyyyyyyyyhhho/:y/my`  ohsso++++++++++oshhs:::::::::::
:::::::::::::::::::::::::::::::::::/::::/:::///++osydddmo./h.`    `/yo:::::://+oyyhhys+/::/od/.hsosyhhhhhys+/://y+mh`  :h..h-..-:::::::-.sd:::::::::::
::::::::::::::::::::::::::::::::::::::/://+syyyyyyyyyssym+/s      /dysssoo+/:--::/osyyhhyo//+dhs/::://////:::://yomd.  .d. y. :s:--:::+s sh:::::::::::
:::::::::::::::::::::::::::::::::::///+syhyysyssyyyyssyh+-/o     .dmdhyyyyys+.-:--::/+syyhhy+oo:/:-..-:::::/:::+ssNd-  `d: y. +o.`-+--.. sh:::::::::::
:::::::::::::::::::::::::::::/::::/osyhyyyysyyyyyyyysyyd+-:s     /Nmmmyyhhysssos+/:-:::/ooshhho/::-..:::::::/::oosmd-   h+ y- `://:`     yh:::::::::::
::::::::::::::::::::::::::::::://syhyysyyyyyyyyyyyyssyyhms-h     ohdmh///oyhysyysso/:--:--oyyyhhs/::/:-::/::/::soymd-   yo y-            yh:::::::::::
:::::::::::::::::::::::::::::/+yhyysysyyyyyyyyyyyyyyyyyd:``y.    ++.+y+/::/+hdyyysyys/:--::/syyyhdo/::-:::::/::soymm-   ys y-            yy::/::::::::
::::::::::::::::::::::::::::+yhyyyyyyyssyyyyyyyyyyyyyyshdy.-y`   -y.+/:oo//:/+hdyssyyso:----:+yyyydh+/::::::/::s+ymm-   ys y-            hy:::::::::::
::::::::::::::::::::::::://shysyysssyyyyyyyyyyyyysyyyyysd+` o+    +h- ``-yo/://smhyyssss+::--:/syyyhdo/:::::/::s+ymm-   ys s:           `hs:::::::::::
::::::::::::::::::::::::/+hhssyyyyyyyyyyyyyyyyyyysyyyysyhhyy.o/    :s/``/:.+s+/:+mdyysssso:----:oyyyydy///:/:::soymm-   yo s:           `hs:::::::::::
::::::::::::::::::::/:::odyysyyyyyyyyyyyyyyyyyyyyyyyyssysyds``+o`   `:oys.`/ho/::/mdyso:oso-:--::oyyyyhh/:::/:/ooymm-   h+ s:           `hs:::::::::::
:::::::::::::::::::::/:sdyyysyyyyyyyyyyyyyyysyyysyyysssyyyds+os:o:``   `-/oyysyyys+mmysyys/.`-:--:++syydh//:/::osymd-   d/ o/           `ds:::::::::::
:::::::::::::::::::::/odyyyysyyyyyyyyyyyyyyyyyyyysyyyysssyyyhmy``:o/`       `.+ho/:+mdyssyo:/-:--:-+yyyydh:::/:+ssmd.  `d- o/           `ds:::::::::::
::::::::::::::::::::/+dysssyyyyyyyyyyyyyyyyyyssydyyyyssyssyysdy+sd:-++/.  `-oyo/:/::oNdysysyy+::-:-/syyyyms::::+yomd.  :h` o/           `do:::::::::::
::::::::::::::::::::/hhyyyyyyyyyyyyssyyyyyyyyysyydhsssyyyyyysyyhhm-`-:.:++hy+//:::/::hNyyyyyys/-:--:+yyyyhm+:/:/yomh` `so  o+           .do://::::::::
::::::::::::::::::::omyyyyyyyyyyyyysyyyyyyyyssssyyhdyssyyyyyysyyyhhhdm:`//hs:::-``-::+mdyysyyso:-::-..+syymy::/:h+my` .h.  ++           .do:::::::::::
::::::::::::::::::::hdysyyyyyyyyyyysyyhsyyyysyysysyydhyyssyyyyyysyyyyhdhmhy/:::::-:://yNyysyo/o/-:--..oyyydm+:/:y+mo `oh-` ++           .do:::::::::::
:::::::::::::::://+odhysyyyyyyyyyyysyyhyyyyyysyyyysyyhdhysyyyyyyyysssyydh/::::://////+oNdyyssss+:----/yyyyhNy++/ssy: :mNdh+so `       ``-d+:::::::::::
:::::::::::::/oshhhhmysysyyyyyyyyyyysshhysyyyyyyyysyyyyhhhyyysssyyyyyyymhooosssoooooooodmysyysys:--::/syyyyNd++++s-`:ydhhmmmmhhhhhhyyyyyhh+::/::::::::
:::::::::::/ohdhyyyhdyyssyssyyyyysyysyydyssyyyyyyyyyyyssyyhhhyyyyyyssshmmmmm+.`:oyhhhddmmhhhhhhho++++oyhhhhmmdhhyyso/````.:+shmmmh+///////::::::::::::
::::::::::/ydhyyyyddmyyyyyysyyyyyyyyssydhsysyssyyyyyyyyyyyyyyyhhhhhyyydmmmmmh. ```...---:::::::/:::::::::::---...```      `:oyhyo/::::::::::::::::::::
:::::::::/ydhyyyhdo-hhsyyyyyyyyyyyyyyssydyyyssyyyyyyyyyyysyyyyyyyyyyyhdmmmmmms`    `+yys:`                              `:yhs+//::::::::::::::::::::::
:::::::::ymyyyyymo`.ddyyyyyyyyyyyyyyyysshdyyssyyyyyyyyyyyysssssyyyyyyyyyhdmmmmy`  `so-./do                          `` .sdo/::::::::::::::::::::::::::
::::::::odhyyyyym/ :ddhyyyyyyyyyyyyyyyyyyhhyyyyyyyyyyyyyyyyyyyyyyssssyssyyhmNmNy+++s`   sh                         ./.-yh+::://:::::::::::::::::::::::
:::::::/hdyyyyyydh-+hydysyssyyyyyyyyyyyyyyhhyyyssyyyyyyyyyyyyyyyyyyyssysyhysdms:.```   :h:                       .//..hh//::::::::::::::::::::::::::::
:::::::+mdyyyyyyyhhdhsyhhysyyyyyyyyyyyyyssyhdysysyyyyyyyyyyyyyyyyyyyysyyho`:s-`        -s:                    .://.``od+::::::::::::::::::::::::::::::
:::://:smhyyyyyyyyydhssyyyyyyyyyyyyyyyyyyyyyydhyssyyyyyyyyyyyyyyyyyysyyyh..y.     ``    -s:::-.         ``.-://-`   .ms:/:::::::::::::::::::::::::::::
:::::::ymyyyyyyyhhdmhsssyyyyssyyyyyyyyyyyyyyyyhhysyysyyyyyyyyyyyyyyysyyhy`:h     /hy`    ````-+/`  :+++++/:-.`      +m///:::::::::::::::::::::::::::::
::::::/ymhyyyyyyyhydhsssyyyysyyyyyyyyyyyyyyyyssyhhyyyssyysyyyyyyyyyysyyyd-.h.   `+mh.   `     .h`  o+.``           `yh/:::::::::::::::::::::::::::::::
:::::::smhyyyyyyyo:/dyyysyyyyyyyyyyyyyyyysssyyyyyyhhyyyyyysyyyyyyyyysssyddsdh.`   .`   .so:--:os`  o/              `yy//::::::::::::::::::::::::::::::
:::::::+mdyyyyyyy+..hhyyyyyyyyyyyyyyyyyyyyyyyyyyyysyyhhhyysyssssyyyyyyyhhmdhhho`  ``   `/s+:::-`   s:              `yy//::::::::::::::::::::::::::::::
::::::::ymhyyyyyyyo/odyyysyyyyyyyyyyyyyyysssyyyyssssssyyhdddhhhhhhhhdddhs+///sy`  /y`    `y:       s:              `sh/:::::::::::::::::::::::::::::::
::::::/:/ymhyyyyyyyyyhdhyyyyssyyyyyyyyyysyyyyyyyyyyhhdddmdysossyyssoo+//::/::y+  `oN+`    oo      `+-               +d/:::::::::::::::::::::::::::::::
:::::/:::/sddhyyyyyyyyhdhyyysyyyyysyyyyyydmmmmmmmmmmmmmmy/::::::::::::::::::/sd+/sdymo-`.:m+------------------/sss:-/mo/:/::::::::::::::::::::::::::::
:::::::::::+sdddhhyyyyyydddhyyyyyyyyyhddmmmmmmmmmmmmmdy+/::::::::::::::::::::/ohysoyy+ossso:------------------/ooo:-:+yho/::::::::::::::::::::::::::::
:::::::::::://+syhhdddhhdddddddddddddhso+ydmmmmmmmdyo//:/::::::::::::::::://::::/oh:```````````````````````````````````/dy/:::::::::::::::::::::::::::
:::::::::::::::/::://////////+ooo++//:::::/+ooo++/:::::::::::::::::::::::::://::+hyssssssssssssssssssssssssssssssssssssoyds:/:/:::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::/:::::::::::::::::::::::::::::::::::://////////////////////////////////////////:/:::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::/::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Name: Anonymous 2012-01-21 8:30

>>9
C has genetic macros only.
They evolve at random when you pass certain arguments to them.

Name: Anonymous 2012-01-21 13:06

>>6
You don't really know what multiple dispatch is, do you.

Name: Anonymous 2012-01-21 14:26

>>11
we can't have fun?

Name: Anonymous 2012-01-21 16:37

>>11
Actually I do, ``faggot''. It's when a different function or method is called depending on the types of all the arguments instead of just the invocant. In case you're a mental midget, (B){} and (C){} are compound literals. The B and C are types. A generic selection in C is the same as compile-time function overloading. If I used f_BB, f_BC, etc. instead of calling puts directly, it would be no different than C++ name mangling. Run-time multiple dispatch in C is usually done by using a tagged union of structs and then calling a different function depending on the type tag of each, or by using an array of function pointers for virtual functions.
#include <stdio.h>

typedef struct {
     enum Type { B, C } T;
     union {
          struct B {int dummy;} B;
          struct C {float dummy;} C;
     };
} Multi;

Multi B_ctr(int n) {return (Multi){B, .B.dummy = n};}
Multi C_ctr(float n) {return (Multi){C, .C.dummy = n};}

void f(Multi x, Multi y) {
     switch (x.T) {
          case B:switch (y.T) {
               case B:puts("BB");
               break;
               case C:puts("BC");
          }
          break;
          case C:switch (y.T) {
               case B:puts("CB");
               break;
               case C:puts("CC");
          }
     }
}

int main(void) {
     Multi B = B_ctr(0);
     Multi C = C_ctr(0);
     f(B,B);
     f(B,C);
     f(C,B);
     f(C,C);
}

Name: Anonymous 2012-01-21 20:56

>>13

Multiple dispatch happens at runtime. Compile time function overloading isn't multiple dispatch. Nothing is being dispatched; the target of a call is fixed at run-time.

Name: Anonymous 2012-01-21 21:59

struct object {
  struct metaclass * class;

  /* other attributes go here */
};

struct metaclass {
  struct object _;

  const char * name;
  const struct metaclass * super;
  size_t size;

  /* function pointers go here */
};

Name: Anonymous 2012-01-21 23:04

4/10.

I would have given this a 2, but like, you all managed to muster enough brain power to talk about something besides SICP.

Name: Anonymous 2012-01-21 23:24

>>16 Hasn't read his SICP today.

Name: Anonymous 2012-01-22 1:52

>>16
SICP doesn't even touch on multiple dispatch.

Name: Anonymous 2012-01-22 9:13

>>1
What happens if you forget to define one combination?

Name: Anonymous 2012-01-22 9:22

>>19
error, just like it would if you tried to call any method in any language with the wrong arguments.

Name: Anonymous 2012-01-22 10:11

>>20
Unless you're using a proper programming language like Haskell.

Name: Anonymous 2012-01-22 10:18

>>20
error, just like it would if you tried to call any method in any language with the wrong arguments.
I'd say it's fairly easy to design a language where you may call a method with the wrong arguments without any error.

Name: Anonymous 2012-01-22 10:30

>>22
with would then lead to access errors or undefined behavior if you were trying to access a certain property of said argument when it's not what you expected

anyway to define a method in which the arguments don't meet the other defined ones you just declare it without specifying the type

(defmethod f (x y) (print "UNKNOWN"))

Name: Anonymous 2012-01-22 10:33

>>19

yeap, in lisp it's a run time error.

Name: >>24 2012-01-22 10:35

or you can do what >>23-san proposes, of course.

Name: Anonymous 2012-01-22 10:54

If lisp is so great why doesn't it have proper OO?

why do I have to do:

(defclass faggot () ())
(defmethod eatshit (f) (doshit))

(eatshit (make-instance 'faggot))


when proper OO should be done via

(defclass faggot () ()
   (defmethod eatshit () (doshit_withthis_object)))

(make-instance 'faggot').eatshit()

Name: Anonymous 2012-01-22 10:59

>>26

because lisp doesn't binary operators like . You call the method on the object, or tupple of objects, and if stuff is getting overriden, it'll get resolved based upon the arguments, rather than only on the type of the thing before the dot. Multiple dispatch could be implemented using either syntax of course.

Name: Anonymous 2012-01-22 11:00

>>26

and be careful when you say proper OO. Just because it doesn't look like seeples or java, doesn't mean it wasn't done this way for many years before those languages even existed.

Name: Anonymous 2012-01-22 11:04

>>26
There are different types of OO.

Name: Anonymous 2012-01-22 11:09

>>26
So, proper OO is all about syntax?

Name: Anonymous 2012-01-22 11:10

what are the advantages of dispatching at run time?

it seems like a waste to me since it has to figure out what each method will actually dispatch to while you run it vs compiler inserting the right method call during compile

Name: Anonymous 2012-01-22 11:14

>>31
Because some of us want true polymorphism?

Name: Anonymous 2012-01-22 11:17

>>26

                                                               ```                                 
                                                          ./ohhddmdy+-                             
                                                       `+hds+:::::/+sdmy/`                         
                                                 `--/++md+::::::::::::/smm/`                       
                                               .shmhNmNdo+::::::::::::::/omh.                      
     ``     `.-.```                ``````     :dmNd+yymmyhs/::::::::::::::/hmo                     
   /dddhossssssyhhddyo-  `./+sss+sdyyyydmhyhhydMmsohy/do:+ymdysss/::::::::::oNdhyyyyoyys:          
  /No:/+//::::::::://oyddmdyo+++oydNd/-:+hdMNymMdoyoy+//.shsss/:/+syyo+/:::::+NMhyyys+/odd:        
  yN:::::::::::::::/+ohds/:::::::::/od+--:ysNd+mh+Ndhyhs.ydd//::://///oyy+::::+Nm/::::::+Nh:       
  sN::::::::::::::::///::::::::::::::+m+--+dyMs/NNydddoyd/hy:::/ossso/://o//:::sNm/:::::+No`       
 .dy::::::///:::::::::::::::::::::::::sd:-/s/Nh/NyymdNd:oyo/::+o+///yms+/o///:::hMy:::///md`       
 .mo:::::::::////:::::::::::::::::::::+N:--s:hdom/+dh/::::+/:::::ossshMNhoyo:+::oMdosyo::yM:       
 -mo::::::+sys+/:///+:::::::::::::::::/N:--:-hd/Nyssy+::::::::::so+shmMMMMms:ho:sMmho/:::+M/       
 .do::::::::/ymmy+/+y+::::::::::::::::sh-----ds:d+os+/:::::::::::/o++sMNhdNsyNsyms//:::::yN-       
  yd::::::::::/hMNhood+::::::::::::::+m+----:mo/ys::::://+++//:::/++ohms:/oyMMNy+:::::::/dh        
  -do::::::::::/dMdNmmNs/:::::::::::+mh-----smydo:::shhso+++++//::oshyyhh+::yMh//:::::::sN:        
   :m/::::::::::oMomNsdNmhs+/++oys:omm/----+mmNo::+dmo/+shhhdmdmdy+/:/sNdho:/dd//::::::+ms         
    hd/:::::::::/NysNy/oydmmdhhyo/oNd/----oNsmh::/mh/+hmdmdshhmhNyNmh+:ommy/:hN//::::::my`         
    -hy::::::::::mh-+dds+////::/odNy:-++/ymooNs::yy/ommddsmhyyymdmhNMhy/dMmmdNN/::::::yN.          
     -dso/:::::::dh-:/ohdddddddhyo/-:y+/mh/:+Ny::h+/NhydysyNydyyddhhNmysNm-:/hM+:::::/Ns           
     .ymyy:::::::hN+:/yssssyyyyso:--/d+oN+::/md::+/+MyhhyhsdmydyhmmhmmNd/.   +Mo:::::sN.           
     `+dNd/::::::sMdyo//+oo+//::----:mh:hms/:yNo:::+MydyhyhymydydydmshdNo    :Ms::::+my            
      `sMm+/::::::ds//+++:-----------/yy/odmmmNN/:++ymdms/yyydhmhhsmdyydm-   :Ms::::yM/            
      `oNMmso/::::odo:-----------------+o::://+mdoomsohNm/+ysdydydysmhssmy   +Mo::::dN:            
      .yMNs//::::::/shh+////:///:----------:::::syhhhhyhMs:hssyydydoomsohN-.-sMs::::dN:            
      -mMs:::::::::/+oyhhhhhs++oyyyyyysoosys+:------:yNhmd:+doosNsmy+ohosmdddhyhyo/:oN+            
      .mMs//:/::::/o++s:////+sso:/ossoso+/::---:/++ymh/`sMo+yhosmdhd++hsoyNo/:::///:+ymyo+-.`      
       /yhyssy///:+oy+/+oso/::+N+----:::::/syhhhdhyo:`  +MssoNsomNymy+osooNy:///::::+sdshNNNdys+/. 
         `:smNdsd+//+h/::/+y+::dmo//:::///+o+/:.``      +Ms+shdoymshNsos+ohNmddhyhy+::oo+ohmNdshdd:
           `/No/+s:::oh::::sm/:/Nh`                     /My:sodyshyommoo++sNh--.`-sdh+:/+sh/omdyoy.
             os:/h/::+m+:::sMdyshN-                     -Nh:osydosyosMso++ohm.     .dm+:/+m++mdmdo 
             -dhyNs//+Ny/:/dNhoymh                       dN+sdsyssssodmooo+ym-      sMy:+hN/+mNNd: 
              oddmhy+hmssy:hmoyds.                       /NhsNysyoshoomyoo+ody      /Nd::ymsms::.  
               .::+ysys-hssmdyNy`                        `hNsmdosoohyosdsooohd     .dMsoomNhNo     
                   .--` /o+ymmh-                          yMdymsooohh++so+oohh     .mN+dymhyo.     
                           `..`                          `ddNsdsooosmo+oo+sohm-     -yhhs-``       
                                                         /Nydshs++yohm+oosoosdd`      ``           
                                                         yNsyyho+/hssNhooyssosN-                   
                                                         oNsoosso/mysmNsoomyosN+                   
                                                         .ddo++do+Nsohyho+mhssm+                   
                                                          :Ny++hoomoohoho+ymssNo                   
                                                          `dm+/ys+msoysyo+hNyyM/                   
                                                          .mMo/ss/hyosssoomMmds`                   
                                                          /Ndh+hy/oyohsosshN+`                     
                                                          oNsdohh+ooyhyosdshm-`                    
                                                         `ddoyohhos+hydssmysms`                    
                                                         .Nhoyomyss/yhNyyyhsds                     
                                                         .mmossdssmoydNhdhyyd-                     
                                                         `dmsooydsNdmdNdsdNd-                      
                                                         :NNdsosNymmNhNmssdN:                      
                                                        `yMdmhosNdNhNmdNsssmy                      
                                                        oNmdymshMmdsmmymmssdm.                     
                                                        hNyyydsmMMhsdd`-hdsdN-                     
                                                        oMhsdhsyNMyosN+ +NyhMo                     
                                                        yMmshhosNMhosmo +MyyNd                     
                                                       /mdmsyNysNdmhdm: sNssdNs                    
                                                     -yhsyhydMhsmd-//.  :NsshhN`                   
                                                     sNhdhshhMhoyM:  .+``dysyhm`                   
                                                     .+smhyydNsosMo`  do ydymhm`                   
                                                       :NssdMyosdM+   yd.dmhddy                    
                                                       /NdNMhosdNMd`  dN-:hdh+`                    
                                                        ./mNsosddMo  +NN+. ``                      
                                                          yNyoosdm` /MddN/                         
                                                          .hmsshN/  :NMydm.                        
                                                           `ohys-    hNdydd.                       
                                                                    :NhdysNh`                      
                                                                   :mhhdsshN/                      
                                                                   +MhNdsshN:                      
                                                                   .mNdmhyNh`                      
                                                                    -hNmdmd-                       
                                                                      :os/`                        

Name: Anonymous 2012-01-22 11:21

The jews are after me.

Name: Anonymous 2012-01-22 11:23

The jews are after me.

Name: Anonymous 2012-01-22 11:25

                                                                                                                                                     
                                                                               `````````           .+.     .                                         
                                                                       `.-:/osyyooohdossos+:.`` .`-:h./`--`y                                         
                                                                   `-+ydmyosydds++.sm.:.:ss/odhy/:/od.o.h+`h:.                                       
                                                               `-+yhhdyhs: ``+///symm+hdddNmsNNNs/dssys:my+dy+`                                      
                                                             `:yhhdh:.-+``.+ydmdy+/:-.-/:`o+`y/+hsho-dsoNddhds:..::                                  
                                                           .sdd/`  ``/.o:odho/-`               /dy/ohdohy/ydyoodd-`                                  
                                                         `odNh-.s.  `-sdo/`                       -`s:yo+h:oN+:.yNy-                                 
                                                        :h+hd-.:s`-+yo/.                            ``.  ` sh+y+do:dy:                               
                                                      `oy-sdsy+.:ss/`                                      `` `/y. .:y+`                             
                                                     .yh/sssy/os+.                                                   `/h/                            
                                                    `ym/+No-do.hd-                                                     `ss`                          
                                                    od: msoms``hNo                                                       +d.                         
                                                   +m/`:NmdNNo:mNo. -oo/  ``                                              oh`                        
                                                   dN+`yNN+yh`/ydo:/+s.:..dh:                                             `h+                        
                                                  /Nh/:h:dNhs-+o::d+oo/dhdN:                                               .h/                       
                                                  sm:+mo-yyodhdds+omh+ `.my    ``                                           .h+                      
                                               -: sMdN/oohMNNN+`omm+yyy-::.`+`hd/                                             sy                     
                                           :+:yhd/yMNMd+sNo//o+dmysNyo:syyhhdy+`     `-/+. `..`  ``     `-y+//+/::-.`         `+y.                   
                                         +o--dm-oNssmmmyodNhh/sd:.s++oNy.smmh-     .+oody`::shd::hm/`     ``.-/:////+o/++-   - `hs                   
                                        `/s:y+y-oNm:.hm/+Ndyh:hm:d+yy`y/`+/:.      :/oo:`.+`/ds-:oyyy+/:.    .syysoso:-+so+- `  :d`                  
                               `.-::.`   od+o-hdNMNhh++hhN/:hm/mdm+:dymhhhoyo`       ` ` oNsNMNymNNmhmNmhs:`  ``````-+o/.``      y+``                
                         `-+syyoo+///ossoyNhs/+++yM/soysshyhmhs:`om+:-hy:/h+-.       :hmomhsmy``-+ssoydNNym/-:::   .-++  `` `-.:odhosso-             
                      `/ss+-``.--`     ``-/omds+//Nh:+oysh:sms//+do/hsym-/-          dNhd+  ``     ``./:o/yhNdho+-ohoo-/ymo+dhhs--/hN+`              
                    .+s/`    `+++o+:`    /o++osdhsNMNhsyhdmNd/sy-sy/s-yo`::   `     .m+`o/    `+ooyhyhmhh+/./ohNNhds-+oyMmyso+/+/` .d`               
                   /y/`           `:y/`   `--/dmdNNh+-````-+hNdhmy+oyyoy.    `:  /:`-d-      `/NNhy+o/++sh+` /`:-sh` `.yd/+oysss+- -m`               
                 `oo.               .s/       .+MN+`  .--.  `:hmmNNyy::-`  .+`   .os:+o:-.-:ohmd:`-yd-//`-sy-`   `:`   `sNy:-:..-+osh                
                .y:`   .o`           -d`       .N/  -oyyshhy:``:.yMy+so`    `   `/-`-`-/ssmNo-d-./dMNmNNo/:hy   .o.     `ys.ys.-` :mo                
               :h:    `yo             so       so  oo.````.omd-  oNms-   `+:``   .``o-///smNy+:-o++/:-.-:.-oo   .h:      `/yNmdh: :d-                
              :h:     /d`             `h/     `h:  m:.ohyy+/ddh` /mys. -/.ss..``/+o/ . ..::..+s/. ```````.:yd    `         `-sh+`.d+                 
             .d:     `h:               /m`     h+  +hds.`-Nd.`o+ /NNo  `o` `/-  -//-        .--+. /oossssshy-                 .osyy`                 
            -y/      +s                `yo    `hh`  oh`  `ms  :y`sMN:   `` `-.              `      ``.oo- `                     `+y-                 
           `h/      `h.                 :m.   .mM-  .m:  oMh` `:`sNm+`  /y`:y                         `                           -s+                
           oo       /y                   h+   -dhs   -`  .ymy.  -mmyy:  `y--s                                                      `+y-              
          -d`       s:                   -h-  `doh-        .-: .yMmys+.`-`/`                                                         /h-             
          oh       .h.                    /d.  hy/h-    `-..   :hsNd+s--s/`.-`.   :`     `                      `````                 :h.            
          dN.      :h                      os  +h`-ys`  +mm+   -/hNNdosmm+-+md+` `d/``...y-   :`           `.:+sossoso/.               +y`           
         :dd/      -h                      .d: :N. `sy` :-:`  :dmmyd/omm/ `sdys/:yNs/sy/y+`   :`   .-    ./hds/-`   `./h:              `d/           
         +o-h.   ``:h                       /h `N:  -m.      `:hmh/+-+syh/ -dhmh+d+-`+-`` ``-s::y+`-+ `:+smd:          :so-  `. -       oy           
        /s` sy+` s:-h                       .h-`dy  `m:     :syhd+/d+/dddo+ddys/s:-:.ho:`-ys-s///.`..+s+-.m-  ````      `:o+` /.-       -m           
......./d:...:/-.h//h........................os-Nm:..+d+...-omN+yyyNhys+d+ymy/+yymd/sh:Nohh:..-++shhyy+s+:N. /hhhy/.``     `  ``        .d:```````````
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyms/.+dddmsss:.`               .ddyyyyyyyyyyy
                                                                                                          .sms+----. ./os/-       ..    `hs...........
                                                                                                            -ohmNMNmyo::/+y.` ``   .`    oh`         
                                                                                                               -:/oooososossossh:.  `    .d.         
                                                                                                                             `-smmy-     /h`         
                                                                                                                                ./yhso::+h-          
                                                                                                                                    `:++-`

Name: Anonymous 2012-01-22 14:08

>>13

Indeed, you do not. That's fine. At least you do not work at Kodak.

...or do you?

Name: Anonymous 2012-01-22 14:34

>>37
Tell that to the guys who wrote the Linux kernel.

Name: Anonymous 2012-01-22 14:36

Here's one way to do it in C. It isn't that difficult to get the multiple
dispatch, but you have to keep the inheritance hierarchy organized by hand,
which is tedious and error prone. You could alternatively generate this code
with a compiler for a superset of C supporting multiple dispatch. I'm not sure
how linking across many libraries that use this technique would work out though.
Each data type would need to get a distinct type id. And what if library X defines
f(B x, B y) and then library Y defines f(B x, D y)? How should the dispatch methods
be merged in this case?


$ cat gen.c
#include <stdio.h>
#include <malloc.h>
#include <assert.h>

enum type {
  type_B,
  type_C
};

typedef enum type* generic_p;

struct B {
  enum type type;
};

void init_B(struct B* self) {
  self->type = type_B;
}

generic_p new_B() {
  struct B* allocation = malloc(sizeof(struct B));
  assert(allocation);
  init_B(allocation);
  return (generic_p)allocation;
}

struct C {
  enum type type;
};

void init_C(struct C* self) {
  self->type = type_C;
}

generic_p new_C() {
  struct C* allocation = malloc(sizeof(struct C));
  assert(allocation);
  init_C(allocation);
  return (generic_p)allocation;
}

void f_BB(struct B* x, struct B* y) {
  printf("BB\n");
}

void f_BC(struct B* x, struct C* y) {
  printf("BC\n");
}

void f_CB(struct C* x, struct B* y) {
  printf("CB\n");
}

void f_CC(struct C* x, struct C* y) {
  printf("CC\n");
}


void f(generic_p x, generic_p y) {
  switch(*x) {
  case type_B:
    switch(*y) {
    case type_B: f_BB((struct B*)x, (struct B*)y); return;
    case type_C: f_BC((struct B*)x, (struct C*)y); return;
    default: assert(0);
    }
  case type_C:
    switch(*y) {
    case type_B: f_CB((struct C*)x, (struct B*)y); return;
    case type_C: f_CC((struct C*)x, (struct C*)y); return;
    default: assert(0);
    }
  default: assert(0);
  }
}

void destroy_B(struct B* self) {
}

void destroy_C(struct C* self) {
}

void destroy(generic_p self) {
  switch(*self) {
    case type_B: destroy_B((struct B*)self); return;
    case type_C: destroy_C((struct C*)self); return;
    default: assert(0);
  }
}

void free_generic(generic_p self) {
  destroy(self);
  free(self);
}



int main(int argc, char** argv) {
  generic_p b = new_B();
  generic_p c = new_C();
  f(b, b);
  f(b, c);
  f(c, b);
  f(c, c);
  free_generic(b);
  free_generic(c);
  return 0;
}
$ gcc gen.c
$ a.out
BB
BC
CB
CC
$ valgrind a.out
==4999== Memcheck, a memory error detector
==4999== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==4999== Using Valgrind-3.5.0-Debian and LibVEX; rerun with -h for copyright info
==4999== Command: a.out
==4999==
BB
BC
CB
CC
==4999==
==4999== HEAP SUMMARY:
==4999==     in use at exit: 0 bytes in 0 blocks
==4999==   total heap usage: 2 allocs, 2 frees, 8 bytes allocated
==4999==
==4999== All heap blocks were freed -- no leaks are possible
==4999==
==4999== For counts of detected and suppressed errors, rerun with: -v
==4999== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 8)

Name: Anonymous 2012-01-22 14:43

>>39
YHBT by >>37
U mad brah?

Name: Anonymous 2012-01-22 14:51

>>40

no, that is a different person, I'm actually the op. I'll probably bump the thread with an implementation in a non oo language once a day for the next week.

Name: Anonymous 2012-01-22 14:51

>>39
That's not multiple dispatch. It's the same thing >>13 did except you split f into separate functions and used malloc instead of local structs. Multiple dispatch is about inheritance. There's no difference between calling f_BB and calling printf("BB\n") unless you can replace f_BB with something else at runtime on an object by object basis. Why do you think >>1 used virtual methods when he could have used overloaded functions with two parameters?
ProTip: use function pointers

Name: Anonymous 2012-01-22 15:02

>>26
That's not proper OO, that's what OO looked after some "modern" languages raped a good concept. If anything, CLOS+MOP and Smalltalk is what ``proper OO'' should all be about.
However, if you really want, implementing that sort of "OO" in CL is very easy, just a few macros and optionally some methods defined with the MOP.

Name: Anonymous 2012-01-22 15:04

>>42

no, that is multiple dispatch. In the main function, the compiler only sees that a and b are of type generic_p, and then f is called on them. Based upon what the types of a and b are at runtime, f will call a different function. It's true that in this example, the compiler could infer that a will point to memory allocated and initialized for a struct A, and b points to memory allocated and initialized for a struct B, but this technique can scale to cases where the compiler wont be able to make such an optimization.

It's true that inheritance is a bitch using this method, but it can be done by managing the switch tables, or generating them from a description of the inheritance hierarchy.

And using a switch statement off of a type flag is one way to get the same functionality obtained with virtual methods. In fact, this is probably the most popular method of getting dynamic dispatch in seeples.

passing around function pointers is probably the most general form for polymorphism, but it isn't always very convenient, or manageable.

Name: Anonymous 2012-01-22 15:21

>>44
No, it isn't. void f(generic x, generic y) { x->f[x->type][y->type](x, y); } (with added error checking) is multiple dispatch. Suppose I added a "D" struct in a different file. How would you override f() to support "D" with your implementation?

Name: Anonymous 2012-01-22 15:51

>>45

by adding a D case to the switch statement under f. It's shit for linking, but it is possible to implement with the right conventions. But the same problem arises when storing a multidimensional array of function pointers.

Name: Anonymous 2012-01-22 15:58

>>45

and also, the compiler could implement the switch statement dispatcher using arrays if it wanted to. It can sometimes be worth the memory savings to use a binary search on a compacted array of integer keys and function pointer values, or binary search on intervals of integer keys with function pointer values. If the number of types becomes large, it could be a problem if the dispatch table takes O(N^A) space, where N is the number of types and A is the arity of the generic method.

Name: Anonymous 2012-01-22 17:05

>>46,47
Switch statements work if you have a small number of predefined classes with a small number of methods, but it's not extensible. You can't inherit from a class in a library using this object system and extend it without modifying the original dispatcher function. A function pointer mechanism is more scalable and extensible than a huge monolithic switch statement per method. You can replace a single member in the struct and get per-object methods. You can use strings or integers for method names and dynamically resolve function pointers without creating separate functions for each. You can use dynamic linking and add in new classes and methods at runtime.

The dispatch mechanism doesn't have to use an array. It just needs to be able to associate types to function pointers and handle inheritance at runtime. It could be a binary tree, compacted array, or array with a pointer to the base class's table. You could even have several predefined types of dispatch table and then use a switch statement to choose at runtime based on how large and sparse the table is.
void f(generic x, generic y) {
     void (*fn)(generic x, generic y) = ResolveFunction("f",x,y);
     if (!fn) abort(); /* failure, no method for those types */
     return fn(x, y);
}

/* in a separate file */
void RegisterD(void) {
     /* one for each combination of types */
     RegisterFunction(&D_vtable,"f",D_type,B_type,f_DB);
     /* ... */
}

Name: Anonymous 2012-01-22 17:22

>>48

do you want it to be extensible at run time or compile time? Because switch statements are extensible at compile time. You just regenerate the c code for the switch statements whenever you update them. You could keep the dispatcher functions in a different file from the various implementations of the multimethod, and each implementation could reside wherever you see fit. If you wanted to change the bindings at run time, you would need to use some kind of data structure like you proposed, but the compiler may be able to better optimize a statically allocated data structure that has been hard coded directly into the code of a subroutine than one that is configurable at run time.

Name: Anonymous 2012-01-22 17:59

Switch statements are extensible at type-in-the-editor-window time.

Name: Anonymous 2012-01-22 18:16

>>50
type-in-the-editor-window time code transformations are too costly.

Name: Anonymous 2012-01-22 18:29

>>50,51

or c code generation time, which can be incorporated into the build process, so it would then be compile time. Doing all of this manually would be tedious and error prone, so one would probably want employ some type of class definition file that would be processed and compiled to headers for the generic data types, and source files for the dispatcher functions.

Name: Anonymous 2012-01-22 18:48

>>50-52
This is why C11 generic selection is useless.

Name: Anonymous 2012-01-22 19:18

>>55
nice dubs bro

Name: Anonymous 2012-01-22 19:54

check 'em
>>54
thanks bro

Name: Anonymous 2012-01-23 21:13

>>55

$ clisp -q
[1]> (defmethod check (digit2 digit1) "wanker!")
#<STANDARD-METHOD (#<BUILT-IN-CLASS T> #<BUILT-IN-CLASS T>)>
[2]> (defmethod check ((digit2 (eql 5)) (digit1 (eql 5))) "ooh yeah!")
#<STANDARD-METHOD ((EQL 5) (EQL 5))>
[3]> (check 5 4)
"wanker!"
[4]> (check 4 5)
"wanker!"
[5]> (check 5 5)
"ooh yeah!"

Nice job!

Name: Anonymous 2012-01-24 1:34

>>56

whoa, I didn't know u could do that! Looks leik pattern matching from de othas, leik ML.

Name: Sensei 2012-01-24 2:23

>> 57

That's EQL specialization on methods.

Method dispatch is a form of pattern matching: pattern matching on types and, as you can see, EQL values.

EQL is more specific than type:

(defmethod foo ((arg symbol)))

(defmethod foo ((arg (eql 'bar)))

(foo 'bar) goes to the second one, but (foo 'other-sym) goes to the first.

Quite useful for dispatching cruft on numbers or symbols (e.g. registering methods keyed to a symbol representing a protocol command or message, etc.)

Name: Anonymous 2012-01-24 10:59

>>1
make-instance
Back to your AbstractCubicleFactoryFactory, ``please"!

Name: Anonymous 2012-01-24 13:04

>>9
Can't see shit.

Don't change these.
Name: Email:
Entire Thread Thread List