Get return type statically

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jun 29 08:10:49 PDT 2016


On Mon, Jun 27, 2016 at 08:13:21PM -0700, H. S. Teoh via Digitalmars-d-learn wrote:
> On Tue, Jun 28, 2016 at 01:41:03AM +0000, Smoke Adams via Digitalmars-d-learn wrote:
> > I have a type
> > 
> > public class SuperFunction(T)
> > {
> >   T t;
> >   return(T) Do() { return t(); }
> > }
> > 
> > where T is a delegate or function. First, I would like to be able to
> > specify that this must be the case for SuperFunction so we can't pass
> > non-function/delegates for T.
[...]
> Maybe this?
> 
> 	import std.traits : ReturnType, Parameters;
> 
> 	ReturnType!T Do(Parameters!T args) { return t(args); }
[...]

Actually, we can do even better by using opCall:

 	ReturnType!T opCall(Parameters!T args) { return t(args); }

Then you can call the object directly:

	auto sf = new SuperFunction!someFunc;
	auto ret = sf(x, y, z);


T

-- 
Кто везде - тот нигде.


More information about the Digitalmars-d-learn mailing list