Created
May 29, 2018 17:40
-
-
Save ben-albrecht/da423b868e771eb2492863591a86ca88 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Dynamic dispatch in Chapel */ | |
{ | |
/* | |
This is not a bug. | |
Chapel only support virtual method dispatch, not multiple dispatch | |
*/ | |
class A { } | |
class B: A { } | |
proc f(arg: A) { | |
writeln('A version'); | |
} | |
proc f(arg: B) { | |
writeln('B version'); | |
} | |
var a = new A; | |
var b = new B; | |
f(a); // calls A version | |
f(b); // calls B version | |
var arr: [1..0] A; | |
arr.push_back(b); | |
f(arr[1]); // calls A version, even though arr[1] is B | |
} | |
{ | |
/* | |
This is not a bug. | |
Chapel does not support dynamic dispatch on paren-less methods | |
*/ | |
class A { | |
proc f { | |
writeln('A version'); | |
} | |
} | |
class B: A { | |
proc f { | |
writeln('B version'); | |
} | |
} | |
var a = new A; | |
var b = new B; | |
a.f; // calls A version | |
b.f; // calls B version | |
var arr: [1..0] A; | |
arr.push_back(b); | |
arr[1].f; // calls A version, even though arr[1] is B | |
} | |
{ | |
/* This is an example of dynamic dispatch on an array of base class type */ | |
class A { | |
proc f() { | |
writeln('A version'); | |
} | |
} | |
class B: A { | |
proc f() { | |
writeln('B version'); | |
} | |
} | |
var a = new A; | |
var b = new B; | |
a.f(); // calls A version | |
b.f(); // calls B version | |
var arr: [1..0] A; | |
arr.push_back(b); | |
arr[1].f(); // calls B version as expected | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment