Help about Template and class inheritance
Heromyth
bitworld at qq.com
Sun Oct 14 13:21:00 UTC 2018
Here is a sample code
```d
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);
// }
}
void main()
{
ThreadPoolExecutor service = new ThreadPoolExecutor();
ExecutorService serviceBase = service;
IExecutorService serviceInterface = service;
Future!(int) f = service.submit!int(12);
writeln(f.result);
f = serviceBase.submit!int(12);
writeln(f.result);
// f = serviceInterface.submit!int(12);
// writeln(f.result);
}
```
The **submit** can't be defined in D as done in Java. See also
(search for exchangeMessageVectorsAsync):
https://www.programcreek.com/java-api-examples/?code=aarmea/noise/noise-master/app/src/main/java/com/alternativeinfrastructures/noise/sync/StreamSync.java
So, is there a better way to do this in D?
Any suggestions are welcome.
Thanks.
More information about the Digitalmars-d-learn
mailing list