Delegates: Difference between revisions

Added Rust
(Added Rust)
Line 2,309:
puts a.delegated # prints "Delegate"
</lang>
 
=={{header|Rust}}==
Requiring delegates to implement Thingable:
<lang Rust>trait Thingable {
fn thing(&self) -> &str;
}
 
struct Delegator<T>(Option<T>);
 
struct Delegate {}
 
impl Thingable for Delegate {
fn thing(&self) -> &'static str {
"Delegate implementation"
}
}
 
impl<T: Thingable> Thingable for Delegator<T> {
fn thing(&self) -> &str {
self.0.as_ref().map(|d| d.thing()).unwrap_or("Default implmementation")
}
}
 
fn main() {
let d: Delegator<Delegate> = Delegator(None);
println!("{}", d.thing());
let d: Delegator<Delegate> = Delegator(Some(Delegate {}));
println!("{}", d.thing());
}</lang>
{{Out}}
<pre>Default implmementation
Delegate implementation</pre>
 
Using nightly-only specialization feature:
<lang Rust>#![feature(specialization)]
 
trait Thingable {
fn thing(&self) -> &str;
}
 
struct Delegator<T>(Option<T>);
 
struct Delegate {}
 
impl Thingable for Delegate {
fn thing(&self) -> &'static str {
"Delegate implementation"
}
}
 
impl<T> Thingable for Delegator<T> {
default fn thing(&self) -> &str {
"Default implementation"
}
}
 
impl<T: Thingable> Thingable for Delegator<T> {
fn thing(&self) -> &str {
self.0.as_ref().map(|d| d.thing()).unwrap_or("Default implmementation")
}
}
 
fn main() {
let d: Delegator<i32> = Delegator(None);
println!("{}", d.thing());
let d: Delegator<i32> = Delegator(Some(42));
println!("{}", d.thing());
let d: Delegator<Delegate> = Delegator(None);
println!("{}", d.thing());
let d: Delegator<Delegate> = Delegator(Some(Delegate {}));
println!("{}", d.thing());
}</lang>
{{Out}}
<pre>Default implementation
Default implementation
Default implmementation
Delegate implementation</pre>
 
=={{header|Scala}}==
Anonymous user