Questions tagged [static-dispatch]

15 questions
9
votes
3 answers

What is the difference between Binding and Dispatching in Java?

There are too many associated names: Early and Late Binding, Static and Dynamic Dispatch, Runtime vs. Compile-time Polymorphism, etc. that I don't understand the difference. I found a clear explanation, but is it correct? I'll paraphrase…
3
votes
0 answers

How do I validate the results of my visitor(variant) static dispatch versus polymorphic dynamic dispatch benchmark?

I have been trying to measure the performance of a std::variant-based (pseudo-) static dispatch scheme using std::visit. The idea is that instead of a struct A { virtual unsigned f() const noexcept = 0; virtual ~A() noexcept {} }; struct B0 :…
bitmask
  • 32,434
  • 14
  • 99
  • 159
2
votes
2 answers

what is impl Trait + 'lifetime

I am reading the async book. In section async lifetimes there is a code snippet whose grammar I am not familiar with: fn foo_expanded<'a>(x: &'a u8) -> impl Future + 'a { async move { *x } } In impl Future + 'a, what…
Steve Lau
  • 658
  • 7
  • 13
2
votes
1 answer

How can I have static dispatch for a linked list containing different types all implementing a trait?

I have this working code: struct Layer<'a> { parent: Option>>, value: Box, } I would like to have a version using static dispatch instead: struct Layer<'a, R: Renderable> { parent: Option<&'a Layer<'a,…
M. Elkh
  • 23
  • 4
2
votes
1 answer

Can we use macros to statically dispatch on a return type in Clojure?

Now we know that dispatch on Clojure Protocols, strictly speaking, is dynamic. We see here a fantastic example of compile-time dispatch using a macro: (defmacro case+ "Same as case, but evaluates dispatch values, needed for referring to class…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
1
vote
1 answer

How to do static dispatch base on derivation on a template

How can I specialized a template function to return true if objects belong to certain templates/classes and false for other templates/classes no classes have virtual functions. I have a template: template struct…
1
vote
0 answers

Implementing Strategy pattern in rust without knowing which strategy are we using at compile time

I've been trying to implement a Strategy pattern in rust, but I'm having trouble understanding how to make it work. So let's imagine we have a trait Adder and Element: pub trait Element { fn to_string(&self) -> String; } pub trait Adder { …
1
vote
0 answers

Override in extension swift - Cannot or Should Not?

Below is my code snippet? extension UISwitch{ open override func draw(_ rect: CGRect) { self.tintColor = UIColor.red } } As per apple documentation, you should not override in extension. I understand this. Now I am not getting an…
0
votes
0 answers

Calling enum_dispatch'd instances of structs from main, outside of crate or file

If run without changes, test_method() is called correctly for both variants in the function get_test_vector(): //! handler.rs use enum_dispatch::enum_dispatch; #[enum_dispatch] trait CommonTrait { fn test_method(&self)…
0
votes
1 answer

When is it appropriate to implement Send + Sync for static dispatch

I find myself playing around with warp. I would like to pass a database Trait to a warp::Filter using static dispatch, such that the concrete database may vary. I read that Send + Sync are unsafe to implement, but I don't fully understand when it is…
kruserr
  • 463
  • 4
  • 8
0
votes
0 answers

Can struct type be static dispatch in swift?

I think that I almost understand what will be static or dynamic dispatch. As far as I know, struct instance with struct type will be always static dispatch. struct A { func yo() { print("A") } } var a: A = A() a.yo() But, struct…
HyunSu
  • 155
  • 7
0
votes
1 answer

Do Rust traits have runtime overhead?

Is there any runtime overhead if I create c1 of type Concrete1 in the code below? pub trait ExampleTrait { fn foo(&self); } pub struct Concrete1 {} impl ExampleTrait for Concrete1 { fn foo(&self) {} } pub struct Concrete2 {} impl…
user855
  • 19,048
  • 38
  • 98
  • 162
0
votes
1 answer

C++ Static Polymorphism––Referencing Specialized Template Methods Overloaded In Derived Class From Base Class Pointer

I am implementing a variation of the observer pattern in C++. However, because of the nature of the nature of my project, it CANNOT USE ANY VIRTUAL MEMBER FUNCTIONS, as the aggregate overhead from vtable lookups and cache misses is…
0
votes
1 answer

Will increasing the use of dynamic dispatch reduce compile time?

In swift the compilation time is really slow the amount of code in your project increases. So i was looking for ways to reduce that time. One approach maybe is to use language keywords like final or static to change the way the compiler handles the…
Mariano
  • 79
  • 6
-3
votes
2 answers

How to create a factory method based on static dispatch?

I want to study design pattern use rust language. From this code it works properly but it uses dynamic dispatch. How can I change the code to use static dispatch? Thanks! trait Shape { fn draw(&self); } enum ShapeType { Rectangle, …