# Dispatch table

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Dispatch_table
> Markdown URL: https://mediated.wiki/source/Dispatch_table.md
> Source: https://en.wikipedia.org/wiki/Dispatch_table
> Source revision: 1335882473
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Computer code structure with a table of pointers to functions or similar}}
In [computer science](/source/computer_science), a '''dispatch table''' is a table of [pointer](/source/pointer_(computer_programming))s or [memory address](/source/memory_address)es to functions or [method](/source/method_(computer_science))s.<ref>{{cite web |last1=Goldfuss |first1=Alice |title=Function Dispatch Tables in C |url=https://blog.alicegoldfuss.com/function-dispatch-tables/ |website=alicegoldfuss.com |access-date=23 January 2021}}</ref>  Use of such a table is a common technique when implementing [late binding](/source/late_binding) in [object-oriented programming](/source/object-oriented_programming).

It is primarily used to simplify program lines and reduce lines of code.<ref>{{Cite web|title=Function Dispatch Tables in C|url=https://blog.alicegoldfuss.com/function-dispatch-tables/|website=Alice Goldfuss|date=2019-03-07|access-date=2026-01-31|language=en|first=Alice|last=Goldfuss}}</ref>

== In different programming languages ==
=== Perl ===
The following shows one way to implement a dispatch table in [Perl](/source/Perl), using a [hash](/source/Associative_array) to store references to code (also known as [function pointer](/source/function_pointer)s).

<syntaxhighlight lang="perl">
# Define the table using one anonymous code-ref and one named code-ref
my %dispatch = (
    "-h" => sub {  return "hello\n"; },
    "-g" => \&say_goodbye
);
 
sub say_goodbye {
    return "goodbye\n";
}
 
# Fetch the code ref from the table, and invoke it
my $sub = $dispatch{$ARGV[0]};
print $sub ? $sub->() : "unknown argument\n";
</syntaxhighlight>

Running this Perl program as <code>perl greet -h</code> will produce "hello", and running it as <code>perl greet -g</code> will produce "goodbye".

=== JavaScript ===
Following is a demo of implementing a dispatch table in JavaScript:
<syntaxhighlight lang="javascript">const thingsWeCanDo = {
    doThisThing() { /* behavior */ },
    doThatThing() { /* behavior */ },
    doThisOtherThing() { /* behavior */ },
    default() { /* behavior */ }
};

function doSomething(doWhat) {
    const thingToDo = Object.hasOwn(thingsWeCanDo, doWhat) 
        ? doWhat 
        : "default";
    return thingsWeCanDo[thingToDo]();
}</syntaxhighlight>

=== Lua ===
In Lua, this behavior is supported and encouraged. Here is an example:<syntaxhighlight lang="lua">
local colors = { -- This is a lookup table improving speed and simplifying program logic
   red = "#ff0000",
   green = "#00ff00",
   blue = "#0000ff"
}

print("Enter a color:")
local choice = io.read("*l")
local color = colors[choice]
if color then
    print("Your color in Hex format is: " .. color)
end
</syntaxhighlight>

== Virtual method tables ==
{{Main|Virtual method table}}
In [object-oriented](/source/object-oriented_programming) [programming language](/source/programming_language)s that support [virtual method](/source/virtual_method)s, the [compiler](/source/compiler) will automatically create a dispatch table for each object of a [class](/source/class_(computer_science)) containing virtual methods. This table is called a [virtual method table](/source/virtual_method_table) or ''vtable'', and every call to a virtual method is dispatched through the vtable.

== See also ==
* [Branch table](/source/Branch_table)

== References ==
{{reflist}}
* [Diomidis Spinellis](/source/Diomidis_Spinellis) (2003). ''[Code Reading](/source/Code_Reading): The Open Source Perspective''. Boston, MA: Addison-Wesley. {{ISBN|0-201-79940-5}}

Category:Method (computer programming)
Category:Articles with example Perl code

---
Adapted from the Wikipedia article [Dispatch table](https://en.wikipedia.org/wiki/Dispatch_table) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Dispatch_table?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
