[SOLVED] How to break this type recursion?

Hi !

So, I want to store some function pointers in a HashMap, but there's one Problem ... the functions need to take a parameter of the same type, which throws a type recursion error.

My desired definition would look like this:

pub type FunctionMap = HashMap<String, fn(&FunctionMap) -> Option<Something>>;

What would be the best way to break this recursion ?

Best,
N

The error message suggests using a struct

struct FunctionMap(...);

instead of a type alias

type FunctionMap = ...;

Ohh ok, this one seems to work:

use std::collections::HashMap;

pub struct FunctionMap {
    pub fmap: HashMap<String, fn(&FunctionMap) -> Option<usize> >
}

impl FunctionMap {
    pub fn new() -> Self {
	FunctionMap {
	    fmap: HashMap::new()
	}
    }
}

fn main() {
    println!("Hello, world!");
    let mut fmap = FunctionMap::new();
    fmap.fmap.insert("get_num_functions".to_string(),
		     |fmap| {println!("this fmap has {} entries", fmap.fmap.len()); Some(fmap.fmap.len())});
}

Guess amongst all the options I tried yesterday, that's the one I missed :sweat_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.