multiple dispatch
1
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
2
Name:
Anonymous
2012-01-21 4:13
>C++
ONE WORD: THE FORCED UGLIFICATION OF CODE
3
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.
4
Name:
>>3
2012-01-21 4:20
is it called RTI? seeples has too many buzzwords/buzzernyms for me.
5
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
6
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){});
}
7
Name:
Anonymous
2012-01-21 4:26
8
Name:
Anonymous
2012-01-21 4:30
>>6
is dat a generic macro? wow
9
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:/:/:::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::/:::::::::::::::::::::::::::::::::::://////////////////////////////////////////:/:::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::/::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
10
Name:
Anonymous
2012-01-21 8:30
>>9
C has genetic macros only.
They evolve at random when you pass certain arguments to them.
11
Name:
Anonymous
2012-01-21 13:06
>>6
You don't really know what multiple dispatch is, do you.
12
Name:
Anonymous
2012-01-21 14:26
13
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);
}
14
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.
15
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 */
};
16
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.
17
Name:
Anonymous
2012-01-21 23:24
>>16 Hasn't read his SICP today.
18
Name:
Anonymous
2012-01-22 1:52
>>16
SICP doesn't even touch on multiple dispatch.
19
Name:
Anonymous
2012-01-22 9:13
>>1
What happens if you forget to define one combination?
20
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.
21
Name:
Anonymous
2012-01-22 10:11
>>20
Unless you're using a proper programming language like Haskell.
22
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.
23
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"))
24
Name:
Anonymous
2012-01-22 10:33
>>19
yeap, in lisp it's a run time error.
25
Name:
>>24
2012-01-22 10:35
or you can do what
>>23- san proposes, of course.
26
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()
27
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.
28
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.
29
Name:
Anonymous
2012-01-22 11:04
>>26
There are different types of OO.
30
Name:
Anonymous
2012-01-22 11:09
>>26
So, proper OO is all about syntax?
31
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
32
Name:
Anonymous
2012-01-22 11:14
>>31
Because some of us want true polymorphism?
33
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/`
34
Name:
Anonymous
2012-01-22 11:21
The jews are after me.
35
Name:
Anonymous
2012-01-22 11:23
The jews are after me.
36
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-
`:++-`
37
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?
38
Name:
Anonymous
2012-01-22 14:34
>>37
Tell that to the guys who wrote the Linux kernel.
39
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)
40
Name:
Anonymous
2012-01-22 14:43
>>39
YHBT by >>37
U mad brah?
Newer Posts