1
0
Fork 0
blog/content/decks/test.html

88 lines
2 KiB
HTML
Raw Normal View History

<section>
2025-05-03 03:37:23 +00:00
<h2>Object (dis)orientation</h2>
<h6>How to survive a "post-OO" world</h6>
</section>
<section>
2025-05-03 03:37:23 +00:00
<section>Class/Inheritance has challenges</section>
<section>
Combining Data + Code
<pre><code data-trim data-noescape data-line-numbers="5|7-9">
class MyBase {
protected:
int useless;
public:
virtual void makeSound() = 0;
virtual int get_thing() {
return -1;
};
};
</code></pre>
2025-05-03 03:37:23 +00:00
<p class="fragment">
You have to know if this is an "interface", abstract class, or regular
class.
</p>
<small class="fragment"
>(it's an abstract class, since <code>makeSound</code> is pure
virtual)</small
>
</section>
<section>
<p>A class can be one of many things</p>
<ul>
<li>Interface - abstract class with no members, and purely virtual</li>
<li>Abstract Class - class with <em>some</em> pure virtual methods</li>
<li>
"Regular" Class - class that is standalone and is fully implemented
</li>
</ul>
<small class="fragment"
>These concepts all exist under the <code>class</code> keyword
</small>
</section>
</section>
<section>
2025-05-03 03:37:23 +00:00
In Rust, you trade <em>Inheritance</em> for <em>Traits</em> and
<em>Types</em>.
</section>
<section>
2025-05-03 03:37:23 +00:00
<section>
<p>Traits are <em>Interfaces</em></p>
<pre><code data-trim data-noescape>
pub trait Mammal {
fn get_temp(&self) -> i32;
}
</code></pre>
2025-05-03 03:37:23 +00:00
</section>
<section>
<p>... that are explicitly implemented</p>
<pre><code data-trim data-noescape>
pub struct Cat {
age: i32,
outdoor: bool,
}
impl Mammal for Cat {
fn get_temp(&self) -> i32 {
if self.outdoor {
65
} else {
72
}
}
}
</code></pre>
2025-05-03 03:37:23 +00:00
</section>
<section>
<p>And can be used statically <em>or</em> dynamically!</p>
<pre><code data-trim>
fn static_mammal&lt;T: Mammal&gt;(m: T) {
// the type of T must be known at compile time.
}
fn dyn_mammal(m: &dyn Mammal) {
// This uses a vtable to dispatch at runtime.
}
</code></pre>
2025-05-03 03:37:23 +00:00
</section>
</section>