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
2
votes
0 answers

Overloading S3 functions with different parameters in R

It is a follow up to the posted question Create a function with different arguments in R I created a generic function but got stuck with passing different set of parameters to these functions modelBuild <- function(x, ...) { …
2
votes
1 answer

Create a function with different arguments in R

I am creating a function that does similar behavior but it would be calling different forecasting algorithms. modelBuild_auto_arima <- function(data, ...) { forecast::auto.arima(data) } modelBuild_ets <- function(data, model, ...) { …
2
votes
0 answers

S3 Method dispatch for ... or equivalent

How do I define a method for a list of objects of the same class? Eg. foo <- letters[1:5] foo2 <- letters[6:10] class(foo) <- "abc" class(foo2) <- "abc" new_method <- function(...) {UseMethod("new_method", ...)} new_method.abc <- function(...)…
alexwhitworth
  • 4,839
  • 5
  • 32
  • 59
2
votes
0 answers

Python: Distinguish if GET (Method-Dispatcher) was called by AJAX or not

i'm stuck with the problem that i can't distinguish between the same overload method GET. i'm using a Method-Dispatcher so i only have GET, PUT, POST and DELETE methods. @cherrypy.expose @cherrypy.tools.json_out() @cherrypy.tools.json_in() def…
2
votes
2 answers

Why target for dynamic dispatch is not determined at compile time?

I am now reading a Java book and stuck for quite a while now thinking about how Dynamic Method Dispatch works. I understand that it's a powerful and valuable feature but I don't understand its main principle. For example, I have a class B which…
Salivan
  • 1,876
  • 7
  • 26
  • 45
2
votes
1 answer

is the Java compile-time dispatch of parameters broken?

I already know that Java dispatches methods based on compile-time types. However I have a case, where I expect that to work, but it doesn't. Consider this simple example: class Foo{ void bar(Object... objects) { //do something } void…
Rafael T
  • 15,401
  • 15
  • 83
  • 144
1
vote
4 answers

Ruby: How do I invoke a function via an object reference?

Consider this contrived example: # Dispatch on value of fruit_kind: TYPE_A = :apple TYPE_B = :banana TYPE_C = :cherry eating_method = nil case fruit_kind # Methods to use for different kinds of fruit (assume these are # already defined) …
Kyle Kaitan
  • 1,761
  • 3
  • 19
  • 29
1
vote
0 answers

canonical meaning of tbl_lazy and related classes

I have an internal package that uses S3 method dispatch to enable lazy operations on what are likely large tables. Currently, I support: "data.frame", in the case where I already downloaded a satisfying subset; "Dataset", derived from…
r2evans
  • 141,215
  • 6
  • 77
  • 149
1
vote
2 answers

Relation between Existential Container and struct instance which conform protocol

I'm trying to understand how to find protocol method's implementation. I know that Swift uses an Existential Container for fixed-size storage in Stack memory which manages how to describe an instance of a struct in memory. and it has a Value Witness…
PrepareFor
  • 2,448
  • 6
  • 22
  • 36
1
vote
1 answer

Error in UseMethod when Method Dispatching

I have tried the following code to create a method but when I use the generic function named "tutu" I receive the following error while the other functions (tutu.num and tutu.ch) work. Please can you help me to understand where is the mistake? I…
Fabio
  • 27
  • 4
1
vote
0 answers

Implementing method dispatch for a R6 class vector

Is there any guidance on how to implement method dispatch for a vector of R6 objects into a dataframe or list? This works beautiful for single R6-classes Proper way to implement S3 dispatch on R6 classes Implementing S3 method dispatch as suggest in…
1
vote
0 answers

NextMethod() explicit argument performance

With the S3 object system, in a generic method you can delegate to the next method in a class hierarchy using the NextMethod() function. When Wickham describes this system at http://adv-r.had.co.nz/S3.html, he uses NextMethod() without any arguments…
Patrick Perry
  • 1,422
  • 8
  • 17
1
vote
2 answers

Java 2 functions with superclass and subclass signatures - chooses superclass despite that subclass's type is subclass

I have the following code: public class Main { public boolean equals(String other){ return other == new Object(); } public boolean equals(Object other){ return other == new Object(); } public static void…
1
vote
1 answer

Type conversion before dispatch takes place

I am defining a generic function genfun <- function(x, ...) UseMethod("genfun") which should have tow instances: genfun.default (if x is a matrix) genfun.formula (if x is a formula) This works fine, but now I would like to have that before the…
Martin
  • 113
  • 1
  • 1
  • 6
1
vote
1 answer

How are interface types loaded by the CLR?

Does the CLR Load interface types if they are not initialized with Concrete implementations? Considering an interface IFoo which has an implementation FooImpl e.g. IFoo foo; as opposed to IFoo foo = new FooImpl(); Will the CLR only load the IFoo…
ganeshran
  • 3,512
  • 7
  • 41
  • 69