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
This is somewhat orthogonal, but it'd be nice to be able to "construct" an instance of a trait that has all of methods defined. This would enable modeling OO-like hierarchies quite conveniently, particularly when combined with specialization.
A rough idea (using dyn to clarify trait objects):
traitWidget{let bounds:Rectangle;fndraw(&self,cx:&mutGraphicsCx);}traitButton:Widget{let text:String;}implButton{fnnew(bounds:Rectangle,text:String) -> Rc<dynButton>{Rc::new(Button{ bounds, text })}// Above is equivalent to:fnnew(bounds:Rectangle,text:String) -> Rc<dynButton>{structDummy{bounds:Rectangle,text:String}implWidgetforDummy{let bounds = self.bounds;}implButtonforDummy{let text = self.text;}Rc::new(Dummy{ bounds, text })}}
default impl<T:Widget>ButtonforT{fndraw(&self,cx:&mutGraphicsCx){
cx.box(self.bounds);
cx.write(self.bounds,self.text);// or whatever}}
More precise rules:
The sugar-y syntax would be available only for traits where all methods have defaults
It effectively returns a impl Button, with one field per (non-defaulted) let member
The text was updated successfully, but these errors were encountered:
As an aside, there are interesting uses for nameable structural _sub_records of a struct of another structural records, like avoiding the verbosity of the builder pattern. If we have pub struct Foo { a: A, b: B, c: C, d: D } then Foo::{b,c} could be a name for the structural record { b: B, c: C } type, so you could write
Is Foo::{b,c} a good name for a type though? Other names might be Foo {b,c} or struct Foo {b,c}. And the choices might interact with this. And the Foo { a, b ..Subrecord } syntax interacts with various ides in the RFC of course.
This is somewhat orthogonal, but it'd be nice to be able to "construct" an instance of a trait that has all of methods defined. This would enable modeling OO-like hierarchies quite conveniently, particularly when combined with specialization.
A rough idea (using
dyn
to clarify trait objects):More precise rules:
impl Button
, with one field per (non-defaulted)let
memberThe text was updated successfully, but these errors were encountered: