Help about Template and class inheritance
Heromyth
bitworld at qq.com
Mon Oct 15 01:26:29 UTC 2018
On Sunday, 14 October 2018 at 15:06:49 UTC, Basile B. wrote:
>
> The basic problem you're faced to here is that D class /
> interface member functions that are templatized are never
> virtual. I'm not sure if i understand well the JAVA pattern you
> try to reproduce but since D interfaces can be templatized you
> can do something like this, using variadics:
>
That's a good try. I want to have a try with template mixin.
About Java's pattern, see also:
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html
The interface ExecutorService has many implements like
AbstractExecutorService, ForkJoinPool,
ScheduledThreadPoolExecutor, ThreadPoolExecutor etc.
The sample above is updated:
import std.stdio;
class Future(T)
{
T result;
this(T r) {
this.result = r;
}
}
interface IExecutorService {
// Future!(T) submit(T)(T result); // can't be implemented
}
abstract class ExecutorService : IExecutorService {
Future!(T) submit(T)(T result) {
return new Future!(T)(result);
}
}
class ThreadPoolExecutor : ExecutorService {
// alias submit = ExecutorService.submit;
// override Future!(T) submit(T)(T result) { // can't override it
// return new Future!(T)(result + 10);
// }
}
class ForkJoinPool : ExecutorService {
// alias submit = ExecutorService.submit;
// override Future!(T) submit(T)(T result) { // can't override it
// return new Future!(T)(result * 10);
// }
}
void testBase(ExecutorService service) {
writeln("testBase: ", typeid(service));
Future!(int) f = service.submit!int(12);
writeln(f.result);
}
void testInterface(IExecutorService service) {
writeln("testInterface: ", typeid(service));
writeln("testInterface: ", typeid(cast(Object)service));
// Future!(int) f = service.submit!int(12);
// writeln(f.result);
}
void main()
{
ThreadPoolExecutor threadPool = new ThreadPoolExecutor();
ForkJoinPool forkJoinPool = new ForkJoinPool();
testBase(threadPool);
writeln();
testBase(forkJoinPool);
writeln("\n");
testInterface(threadPool);
writeln();
testInterface(forkJoinPool);
}
More information about the Digitalmars-d-learn
mailing list