Help about Template and class inheritance

Basile B. b2.temp at gmx.com
Sun Oct 14 15:06:49 UTC 2018


On Sunday, 14 October 2018 at 13:21:00 UTC, Heromyth wrote:
> 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.

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:

import std.stdio;

class Future(T)
{
	T result;

	this(T r) {
		this.result = r;
	}
}

interface IExecutorService(Ts...) {
	static foreach(T; Ts)
	 	Future!T submit(T result);
}

abstract class ExecutorService(Ts...) : IExecutorService!Ts {
	static foreach(T; Ts)
	 	Future!T submit(T result) {
			return new Future!(T)(result);
		}
}

class ThreadPoolExecutor : ExecutorService!int {
	 override Future!int submit(int result) {
	 	return new Future!int(result + 10);
	 }
}

void main()
{
	ThreadPoolExecutor service = new ThreadPoolExecutor();
	ExecutorService!int serviceBase = service;
	IExecutorService!int serviceInterface = service;

	Future!(int) f = service.submit(12);
	writeln(f.result);

	f = serviceBase.submit(12);
	writeln(f.result);

	f = serviceInterface.submit(12);
	writeln(f.result);
}

so that you can submit and have Future for several types.


More information about the Digitalmars-d-learn mailing list