templates - How to use parentheses for generics? -


i'm getting compile error:

angle-bracket notation not stable when used fn family of traits, use parentheses [e0215]

what mean? how "use parentheses"?

use std::hash::hash; use std::collections::hashmap;  struct memoedfun<a, r> {     fun: fn(&a) -> r,     map: hashmap<a, r>, }  fn memoize<a: eq + hash, r>(fun: fn(&a) -> r) -> memoedfun<a, r> {     memoedfun {         fun: fun,         map: hashmap::new(),     } }  impl<'a, a, r> fnonce<a> &'a memoedfun<a, r> {     type output=&'a r; } 

there several problems code.

first of all, cannot use fn* traits directly in stable rust. includes 1) using angle-brackets notation, , 2) implementing these traits. possible enable feature flag both of these things in unstable rust though.

second, if use angle brackets closure traits, have use tuples arguments, if there 1 argument:

fnonce<(a,)> 

third, error message instead of fnonce<(t, u), output=v> should write fnonce(t, u) -> v. that's meant under "using parentheses" message. agree message misleading here because can't implement fn when written because of associated types. guess error implementing fn types should take precedence on error.

fourth, won't able want (a memoizing function backed hash map) when use &'a memoedfun<a, r> because need mutable pointer update map. need implement fnonce &'a mut memoedfun<a, r>:

impl<'a, a: eq + hash, r> fnonce<(a,)> &'a mut memoedfun<a, r> {     type output = &'a r;      extern "rust-call" fn call_once(self, (arg,): (a,)) -> &'a r {         if self.map.contains_key(&arg) {             &self.map[&arg]         } else {             let r = (self.fun)(&arg);             self.map.entry(arg).or_insert(r)         }     } } 

and finally, resulting code have write use memoizer isn't pretty. can't use function syntax on "function" reason, need use call_once() directly:

fn computer(x: &i32) -> i32 {     println!("computing {}", x);     -*x }  let mut f = memoize(computer);  println!("f(10): {}", (&mut f).call_once((10,))); println!("f(10): {}", (&mut f).call_once((10,))); println!("f(42): {}", (&mut f).call_once((42,))); 

(try here)

there reason why fn* traits manual implementation not stabilized, after all.


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -