implement vs override
Peter C
peterc at gmail.com
Mon Nov 3 22:28:31 UTC 2025
On Monday, 3 November 2025 at 12:29:44 UTC, Quirin Schroll wrote:
>
> ..
Borrowing and idea from Carbon and Rust, you could group all the
interface methods together in a dedicated block/scope using
'impl':
It's nice and structured, and would scale very well for handling
multiple interfaces.
module myModule;
@safe:
private:
import std.stdio : writeln;
interface IWorker
{
void start();
void stop();
int status();
}
class Base
{
void start()
{
writeln("Base: starting generic process");
}
void stop()
{
writeln("Base: stopping generic process");
}
int status()
{
return 0; // 0 = idle
}
}
class Derived : Base, IWorker
{
// Explicit interface implementation block
impl(IWorker)
{
void start()
{
writeln("IWorker: initializing hardware");
}
void stop()
{
writeln("IWorker: shutting down hardware");
}
int status()
{
writeln("IWorker: reporting hardware status");
return 42; // pretend 42 = "active"
}
}
}
void main()
{
auto d = new Derived();
// Calls Base methods
d.start(); // "Base: starting generic process"
d.stop(); // "Base: stopping generic process"
writeln(d.status()); // 0
// Calls IWorker methods
IWorker w = d;
w.start(); // "IWorker: initializing hardware"
w.stop(); // "IWorker: shutting down hardware"
writeln(w.status()); // 42
}
More information about the Digitalmars-d
mailing list