How to group similar member functions from different classes?

cy via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 18 00:03:25 PDT 2016


When I define functions like:

class A {
   abstract void format(...) {...}
}

class B : A {
   void format(...) {...}
}

class C : A {
   void format(...) {...}
}

and so on, often these different member functions all share a lot 
in common. Maybe they are the only ones that require formatting 
modules, that do I/O, that do string manipulation, that sort of 
thing.

But if I made a second function, say for instance clone() or 
replaceWithDucks(), it too might import a lot of modules, and 
perform a lot of logic. And there may be many, many different 
types of object here.

In C++ I could forward declare the member functions, then put all 
"::format(...)" member functions together in their own source 
file, making everything pretty neat and tidy. How do I do that in 
D? As near as I can tell, you can only define member functions 
inside the class definition itself, and you can't add to that 
definition piece-wise like

// file 1
class A ... {
   void format(...) { ... }
}
...

// file 2
class A ... {
  void doathing() { ... }
}
...

So how would you do it? Defining A.foo, B.foo, etc in one place, 
and A.bar, B.bar, etc in another?


More information about the Digitalmars-d-learn mailing list