You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was looking into using references instead of passing around a context, and it seems tricky. Just wanted to record somewhere how far I got.
This is close, but doesn't quite work:
structState<F,V>{f:F,state:String,phantom: std::marker::PhantomData<V>,}impl<'a,V:View + 'a,F:Fn(&'a String) -> V + 'a>ViewforState<F,V>{fndraw(&self){(self.f)(&self.state).draw();}}
The problem is in F: Fn(&'a String) -> V + 'a. This says the entire closure cannot outlive the String passed in. That's not quite what we want. It's ok if the closure lives longer. What we want is for the return type (V) to live as long as the String passed in.
What we want is something like F: for<'a> Fn(&'a String) -> V<'a> where V is a higher-kinded-type, but rust doesn't (yet) have this feature.
The text was updated successfully, but these errors were encountered:
This works, at the cost of boxing views, and process has to return a new value because it can't get a mutable reference:
traitView{fndraw(&self);fnprocess(&self,data:&String) -> Option<String>;}structState<F>{f:F,}impl<F>ViewforState<F>whereF:for<'a>Fn(&'a String) -> Box<dynView + 'a>,{fndraw(&self){// Get the state from somewhere.let s = "hello world".to_string();(self.f)(&s).draw();}fnprocess(&self,_data:&String) -> Option<String>{letmut s = "hello world".to_string();let r = {let v = (self.f)(&s);
v.process(&s)};ifletSome(n) = r {
s = n;}None}}structEmpty{}implViewforEmpty{fndraw(&self){}fnprocess(&self,_data:&String) -> Option<String>{None}}fnstate<'b,F:'b + for<'a>Fn(&'a String) -> Box<dynView + 'a>>(f:F) -> Box<dynView + 'b>{Box::new(State{ f })}
I was looking into using references instead of passing around a context, and it seems tricky. Just wanted to record somewhere how far I got.
This is close, but doesn't quite work:
The problem is in
F: Fn(&'a String) -> V + 'a
. This says the entire closure cannot outlive the String passed in. That's not quite what we want. It's ok if the closure lives longer. What we want is for the return type (V
) to live as long as the String passed in.What we want is something like
F: for<'a> Fn(&'a String) -> V<'a>
whereV
is a higher-kinded-type, but rust doesn't (yet) have this feature.The text was updated successfully, but these errors were encountered: