Jump to page: 1 2
Thread overview
Exactly Replicating a Function's Signature?
Jan 19, 2011
%u
Jan 19, 2011
bearophile
Jan 19, 2011
%u
Jan 19, 2011
Andrej Mitrovic
Jan 19, 2011
Jacob Carlborg
Jan 19, 2011
Andrej Mitrovic
Jan 19, 2011
%u
Jan 19, 2011
Jacob Carlborg
Jan 20, 2011
%u
Jan 20, 2011
Andrej Mitrovic
Jan 20, 2011
%u
Jan 19, 2011
Andrej Mitrovic
Jan 19, 2011
wrzosk
Jan 19, 2011
Andrej Mitrovic
Jan 19, 2011
Andrej Mitrovic
Jan 19, 2011
Andrej Mitrovic
Jan 19, 2011
Andrej Mitrovic
January 19, 2011
If I have an alias F that represents a function, is there any way for me to create a function func() whose signature is exactly the same as that of F?

This includes parameter types, return types, and any/all storage modifiers (e.g. const, lazy, scope, etc.).

Without this capability, it's impossible to perform perfect generic redirection for functions that pass arguments by reference or in a lazy fashion, right?
January 19, 2011
%u:

> If I have an alias F that represents a function, is there any way for me to create a function func() whose signature is exactly the same as that of F?

In theory this has to be enough:
typeof(F) F2;

Bye,
bearophile
January 19, 2011
> In theory this has to be enough: typeof(F) F2;

But in practice, I want to change the body of my function, or possibly add new parameters in the beginning or the end. Does this let me do either of these?

Thank you!
January 19, 2011
Is this what you're looking for?:

import std.stdio;
import std.traits;

void test(alias F)()
{
    ReturnType!(F) otherFunc(ParameterTypeTuple!(F))
    {
        typeof(return) result;
        return result;
    }
}

double foo(int x, int y)
{
    double result;
    return x + y * 2.5;
}

void main()
{
    test!foo;
}
January 19, 2011
Sorry, here's a more complete example:

import std.stdio;
import std.traits;

void test(alias F)(int x, int y)
{
    ReturnType!(F) otherFunc(ParameterTypeTuple!(F) args)
    {
        typeof(return) result;

        result = args[0] + args[1] * 2.5;

        return result;
    }

    writeln(otherFunc(x, y));
}

double foo(int x, int y)
{
    double result;
    return x + y * 2.5;
}

void main()
{
    test!(foo)(4, 5);
}
January 19, 2011
Better:

void test(alias F)(ParameterTypeTuple!(F) args)
{
    ReturnType!(F) otherFunc(ParameterTypeTuple!(F) args)
    {
        typeof(return) result;

        result = args[0] + args[1] * 2.5;

        return result;
    }

    return otherFunc(args);
}

double foo(int x, int y)
{
    double result;
    return x + y * 2.5;
}

void main()
{
    writeln(test!(foo)(4, 5));
}
January 19, 2011
Holy cow is that a bug? It's returning a value even though it's a void function.
January 19, 2011
Never mind my stupid build script didn't recompile and it ran an old version. Fixed:

import std.stdio;
import std.traits;

auto test(alias F)(ParameterTypeTuple!(F) args)
{
    ReturnType!(F) otherFunc(ParameterTypeTuple!(F) args)
    {
        typeof(return) result;

        result = args[0] + args[1] * 2.5;

        return result;
    }

    return otherFunc(args);
}

double foo(int x, int y)
{
    double result;
    return x + y * 2.5;
}

void main()
{
    writeln(test!(foo)(4, 5));
}
January 19, 2011
On 2011-01-19 16:29, Andrej Mitrovic wrote:
> Is this what you're looking for?:
>
> import std.stdio;
> import std.traits;
>
> void test(alias F)()
> {
>      ReturnType!(F) otherFunc(ParameterTypeTuple!(F))
>      {
>          typeof(return) result;
>          return result;
>      }
> }
>
> double foo(int x, int y)
> {
>      double result;
>      return x + y * 2.5;
> }
>
> void main()
> {
>      test!foo;
> }

Will that keep: ref, out, inout and so on in the signature ?

-- 
/Jacob Carlborg
January 19, 2011
I never thought of that. There's a ParameterStorageClassTuple template, but I don't see a template that combines both of these templates to duplicate the exact signature. Something like that should probably be added to Phobos.
« First   ‹ Prev
1 2