Questions tagged [method-dispatch]

For questions and answers related to dynamically calling object methods at runtime

Overview

Method dispatch is a fundamental concept in many programming languages that allows objects to dynamically call methods at runtime.

In Python, the getattr function provides a way to retrieve attributes and methods from an object by name, and dynamically invoke them using the parentheses operator.

Similarly, in Ruby, the send method allows objects to invoke methods with the same name as a given symbol.

Other programming languages, such as Java and C++, use a similar mechanism called "virtual method dispatch", which dynamically selects the correct implementation of a method based on the type of the object at runtime.

Method dispatch is a key aspect of object-oriented programming, enabling polymorphism and dynamic behavior in complex software systems.

See also

Wikipedia

38 questions
133
votes
3 answers

When should I use @classmethod and when def method(self)?

While integrating a Django app I have not used before, I found two different ways to define functions inside the class. The author seems to use them both distinctively and intentionally. The first one is the one that I myself use a lot: class…
marue
  • 5,588
  • 7
  • 37
  • 65
117
votes
5 answers

Using -performSelector: vs. just calling the method

I'm still kind of new to Objective-C and I'm wondering what is the difference between the following two statements? [object performSelector:@selector(doSomething)]; [object doSomething];
TheGambler
  • 3,711
  • 5
  • 38
  • 54
53
votes
4 answers

Call method from string

If I have a Python class, and would like to call a function from it depending on a variable, how would I do so? I imagined following could do it: class CallMe: # Class def App(): # Method one ... def Foo(): # Method two …
Sirupsen
  • 2,075
  • 2
  • 19
  • 24
24
votes
5 answers

Overloading is compile-time polymorphism. Really?

I do know the syntactical difference between overriding and overloading. And I also know that overriding is run-time polymorphism and overloading is compile-time polymorphism. But my question is: "Is overloading is really compile-time polymorphism?…
Jomoos
  • 12,823
  • 10
  • 55
  • 92
13
votes
4 answers

Java method dispatch with null argument

Why does it (apparently) make a difference whether I pass null as an argument directly, or pass an Object that I assigned the value null? Object testVal = null; test.foo(testVal); // dispatched to foo(Object) // test.foo(null); // compilation…
Yang Meyer
  • 5,409
  • 5
  • 39
  • 51
10
votes
1 answer

At runtime, how does Swift know which implementation to use?

protocol A { func f() } struct S1 : A { func f() { print("S1") } } struct S2 : A { func f() { print("S2") } } let array: [A] = [S1(), S2()] for s: A in array { s.f() } // "S1\n" "S2\n" If this was an…
Ian Warburton
  • 15,170
  • 23
  • 107
  • 189
7
votes
2 answers

PHP equivalent of send and getattr?

If Ruby gets invited to a party and brings: foobarobject.send('foomethod') .. and Python gets invited to the same party and brings: getattr(foobarobject, 'foomethod')() .. what does PHP have to bring to the party? Bonus question: If Ruby and…
dreftymac
  • 31,404
  • 26
  • 119
  • 182
4
votes
1 answer

R's S3 method dispatching does not work when sourcing package

I am currently developing a package in R with the "help" of devtools. That is, the package is getting loaded into R studio via load_all(path = ...) in this stage and my functions from the R directory are made available. So far so good. But when I…
gero
  • 119
  • 11
4
votes
2 answers

Determine arity of method with keyword arguments

I am developing a Ruby application where I am dynamically invoking methods based on JSON data. Loosely: def items # do something end def createItem( name:, data:nil ) # do something that requires a name keyword argument end def…
Phrogz
  • 296,393
  • 112
  • 651
  • 745
4
votes
1 answer

Method dispatch with missing arguments

How can I avoid the classic Error: argument "" is missing, with no default error (see example below) when explicitly dispatching argument values to subsequent S4 methods in a given S4 method. Example Big picture Whe have a method foo()…
Rappster
  • 12,762
  • 7
  • 71
  • 120
3
votes
1 answer

Does marking a Swift class final also make all contained vars, lets and functions gain Static Dispatch benefits automatically?

I am trying to squeeze every last bit of performance out of my app. I try to use Structs over classes wherever possible (no state sharing, direct dispatch by default, etc etc). But my view controllers and UIView objects are obviously still…
FranticRock
  • 3,233
  • 1
  • 31
  • 56
3
votes
2 answers

Work around Java's static method dispatching without Double Dispatch/Visitor patterns

I am using a class Foo that provides these methods: String overloadedMethod(Object) String overloadedMethod(Goo) Since Java statically dispatches on the non-receiver argument, I cannot just pass my value (which is an Object, but might have dynamic…
2
votes
1 answer

How does R find S3 methods? Why can't R find my S3 `+` method?

I would like to create and attach an environment containing S3 methods and have R find them in the search path, by using the method name (i.e., I should be able to get infix-style a + b to work and not have to write prefix-style "+.Foo"(a, b)). What…
Ana Nimbus
  • 635
  • 3
  • 16
2
votes
3 answers

Java dynamic binding

I am practicing for an exam, and found a sample problem that gets me totally lost. For the following code, find what the output is: class Moe { public void print(Moe p) { System.out.println("Moe 1\n"); } } class Larry extends Moe { …
Nathan H
  • 48,033
  • 60
  • 165
  • 247
2
votes
0 answers

S3 method dispatch with S4 methods

I have some trouble understanding the differences in method dispatching between S3 and S4 classes. As far as I understand, S3 classes use UseMethod and finds the correct method via the class attribute of the object passed. S4 classes use…
ahanf
  • 59
  • 1
  • 7
1
2 3