Jump to page: 1 2
Thread overview
How to declare function with the same call signature as another?
Nov 20, 2016
Tofu Ninja
Nov 20, 2016
Nicholas Wilson
Nov 20, 2016
Tofu Ninja
Nov 20, 2016
Tofu Ninja
Nov 23, 2016
Tofu Ninja
Nov 23, 2016
ketmar
Nov 23, 2016
Tofu Ninja
Nov 23, 2016
ketmar
Nov 23, 2016
Tofu Ninja
Nov 23, 2016
ketmar
Nov 24, 2016
Tofu Ninja
Nov 24, 2016
ketmar
Nov 24, 2016
Tofu Ninja
Nov 24, 2016
ketmar
Nov 24, 2016
Tofu Ninja
Nov 24, 2016
ketmar
Nov 24, 2016
Tofu Ninja
Nov 24, 2016
ketmar
Nov 20, 2016
Nicholas Wilson
November 20, 2016
I feel like this should be simple but I can't seem to figure it out. How do I declare a function to have the same call signature as another function/callable type?

Like if I have:

alias Sig = int function(int x, int y);

How do I define a function such that it will have the same call signature as Sig? How do I take into account all the extra stuff that can go into a function signature like the argument attributes and the return type attributes etc.?

Another way to phrase this question would be, how do I pass a function signature into a template and actually define functions with it?

Related question, if I have Sig, how would I define DeltaSig to be exactly the same as Sig but with an extra parameter at the start or end of the parameter list?

Thanks :)


November 20, 2016
On Sunday, 20 November 2016 at 11:19:24 UTC, Tofu Ninja wrote:
> I feel like this should be simple but I can't seem to figure it out. How do I declare a function to have the same call signature as another function/callable type?
>
> Like if I have:
>
> alias Sig = int function(int x, int y);
>
> How do I define a function such that it will have the same call signature as Sig? How do I take into account all the extra stuff that can go into a function signature like the argument attributes and the return type attributes etc.?
>
> Another way to phrase this question would be, how do I pass a function signature into a template and actually define functions with it?
>
> Related question, if I have Sig, how would I define DeltaSig to be exactly the same as Sig but with an extra parameter at the start or end of the parameter list?
>
> Thanks :)
import std.traits;
ReturnType!Sig func(Parameters!Sig args)
{
    //...
}
November 20, 2016
On Sunday, 20 November 2016 at 11:19:24 UTC, Tofu Ninja wrote:
> I feel like this should be simple but I can't seem to figure it out. How do I declare a function to have the same call signature as another function/callable type?
>
> Like if I have:
>
> alias Sig = int function(int x, int y);
>
> How do I define a function such that it will have the same call signature as Sig? How do I take into account all the extra stuff that can go into a function signature like the argument attributes and the return type attributes etc.?
>
> Another way to phrase this question would be, how do I pass a function signature into a template and actually define functions with it?
>
> Related question, if I have Sig, how would I define DeltaSig to be exactly the same as Sig but with an extra parameter at the start or end of the parameter list?
>
> Thanks :)

Or if you want additional parameters.

ReturnType!Sig func(int paramBefore, Parameters!Sig args, int paramAfter) { ... }
November 20, 2016
On Sunday, 20 November 2016 at 11:23:37 UTC, Nicholas Wilson wrote:
> On Sunday, 20 November 2016 at 11:19:24 UTC, Tofu Ninja wrote:
>> I feel like this should be simple but I can't seem to figure it out. How do I declare a function to have the same call signature as another function/callable type?
>>
>> Like if I have:
>>
>> alias Sig = int function(int x, int y);
>>
>> How do I define a function such that it will have the same call signature as Sig? How do I take into account all the extra stuff that can go into a function signature like the argument attributes and the return type attributes etc.?
>>
>> Another way to phrase this question would be, how do I pass a function signature into a template and actually define functions with it?
>>
>> Related question, if I have Sig, how would I define DeltaSig to be exactly the same as Sig but with an extra parameter at the start or end of the parameter list?
>>
>> Thanks :)
> import std.traits;
> ReturnType!Sig func(Parameters!Sig args)
> {
>     //...
> }

This does not seem to account for return type attributes or function attributes. Eg:

alias Sig = ref int function() @nogc;
ReturnType!Sig func(Parameters!Sig args) {
	// func is missing ref on return type and is not @nogc
	static int g;
	return g;
}

How should I account for these things?

November 20, 2016
On Sunday, 20 November 2016 at 11:52:01 UTC, Tofu Ninja wrote:
> ...

Also does not include function linkage :/
November 23, 2016
On Sunday, 20 November 2016 at 12:06:15 UTC, Tofu Ninja wrote:
> On Sunday, 20 November 2016 at 11:52:01 UTC, Tofu Ninja wrote:
>> ...
>
> Also does not include function linkage :/

Because of the lack of response, I am going to guess there is no way to do this cleanly. Guess I am going to have to break out the trusty old mixin to get this working.


Also wtf is this... how does this even make sense?

template make_ref(T){
    static if(is(void delegate(ref T) ftype == delegate) && is(ftype P == function))
        alias make_ref = P;
    else static assert(false);
}

void main(){
	import std.stdio;
	writeln(make_ref!int.stringof); // (ref int)
}

What is a (ref int)? A tuple with "ref int" as its only member? Since when is ref int a type?
November 23, 2016
On Wednesday, 23 November 2016 at 22:14:25 UTC, Tofu Ninja wrote:
> What is a (ref int)? A tuple with "ref int" as its only member? Since when is ref int a type?

it is "type with modifier", like "const int" or "immutable int".
November 23, 2016
On Wednesday, 23 November 2016 at 22:19:28 UTC, ketmar wrote:
> On Wednesday, 23 November 2016 at 22:14:25 UTC, Tofu Ninja wrote:
>> What is a (ref int)? A tuple with "ref int" as its only member? Since when is ref int a type?
>
> it is "type with modifier", like "const int" or "immutable int".

Since when has ref been a type qualifier? It has always been a parameter/function attribute.
November 23, 2016
On Wednesday, 23 November 2016 at 22:28:57 UTC, Tofu Ninja wrote:
> On Wednesday, 23 November 2016 at 22:19:28 UTC, ketmar wrote:
>> On Wednesday, 23 November 2016 at 22:14:25 UTC, Tofu Ninja wrote:
>>> What is a (ref int)? A tuple with "ref int" as its only member? Since when is ref int a type?
>>
>> it is "type with modifier", like "const int" or "immutable int".
>
> Since when has ref been a type qualifier? It has always been a parameter/function attribute.

which is technically type qualifier. it just forbidden (in grammar) to use it anywhere except arg declaration.
November 23, 2016
On Wednesday, 23 November 2016 at 22:48:17 UTC, ketmar wrote:
> On Wednesday, 23 November 2016 at 22:28:57 UTC, Tofu Ninja wrote:
>> On Wednesday, 23 November 2016 at 22:19:28 UTC, ketmar wrote:
>>> On Wednesday, 23 November 2016 at 22:14:25 UTC, Tofu Ninja wrote:
>>>> What is a (ref int)? A tuple with "ref int" as its only member? Since when is ref int a type?
>>>
>>> it is "type with modifier", like "const int" or "immutable int".
>>
>> Since when has ref been a type qualifier? It has always been a parameter/function attribute.
>
> which is technically type qualifier. it just forbidden (in grammar) to use it anywhere except arg declaration.

Maybe the compiler sees it as a type qualifier, but it is not listed as a type qualifier and does not behave like a type qualifier in any sense. For example typeof will never return "ref int" but will return "const int", auto will never infer ref but can infer const, you can pass const(int) into a template but can never pass ref(int) into a template(even with that hack I posted before, the ref gets striped).

Being able to get an alias to (ref int) seems like a bug.
« First   ‹ Prev
1 2