Generic Parameters conditional check

Say I have:

fn <T: Foo>() {


How can I add a conditional in the function to check what Foo is?

What do you mean by check what Foo is? Also, what is the context of this, as though this function is not named, therefore leading one to believe that it’s a function pointer, but generic function pointers don’t exist.

In general though, we cannot include inline code in a function to be conditional over types because there isn’t any type info at runtime and it would break if I tried to say something like the following in pseudocode:

trait Bar {}
struct A;
struct B;

fn foo<T: Bar>() {
    if T == A {
        println!("Got A!");
    } else { // T == B
        println!("Got B!");

fn foo2<T: Bar>() {

fn other<T: Bar>(x: &dyn Fn<T>() -> ()) {x()}

How would the compiler know what function is provided to other and therefore be able to statically ensure one or the other bit of code runs. Therefore, it would need to figure it out at runtime, but unfortunately, there is no type data (Other than the typeid) at runtime, so this is impossible.

You add methods to the Foo trait to tell you all the information you need. If you want to know whether Foo is actually impl Foo for SomeType, then make foo.am_i_some_type() method.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.