September 09, 2021

On Thursday, 9 September 2021 at 12:49:16 UTC, Basile B. wrote:

>

Compilers traits dont require a DIP (IIRC) so just go on with a PR and be
smart when answering the review comments.

You can already get the parameter list of a function using is expression.

Smth like:

is(func Params == _parameters)

But it is a bit buggy, since you have to play a bit with it, before you can even extract the values.

Imho, it would be better to improve the is expression rather than adding a new trait if possible.

Best regards,
Alexandru.

September 09, 2021
On Thursday, 9 September 2021 at 13:49:55 UTC, Alexandru Ermicioi wrote:
> You can already get the parameter list of a function using is expression.

parameters are not arguments.

> But it is a bit buggy, since you have to play a bit with it, before you can even extract the values.

the is expression works perfectly fine, it is just a different thing. parameters are static information about the function's outside. arguments are dynamic data inside the function.

You can get to arguments through parameters though like I mentioned in the link in the OP though. A reasonable implementation is this:

---
public static import std.meta;
template ARGS(alias a /* always pass the first argument to your function to this */) {
        string helper() {
                string code = "std.meta.AliasSeq!(";
                static if(is(typeof(__traits(parent, a)) Params == __parameters))
                foreach(idx, param; Params) {
                        if(idx)
                                code ~= ", ";
                        code ~= __traits(identifier, Params[idx .. idx + 1]);
                }
                else static assert(0);
                code ~= ")";
                return code;
        }

        enum string ARGS = helper();
}
---


And then you can use it like:

---
import std.stdio;

void foo(int a) {
        foreach(item; mixin(ARGS!(a)))
                writeln(item);

        // forwarding can be done like this too:
        foo("omg", mixin(ARGS!a));
}

void foo(string a, int b) {
        foreach(item; mixin(ARGS!(a)))
                writeln(item);
}

void main() {
        foo(4);
        foo("omg", 55);
}
---


This implementation doesn't work on variadic templates but that's not so important since they are already a tuple.


Still though, it just would be nicer if it just worked with a builtin.
September 09, 2021
On Thursday, 9 September 2021 at 08:39:05 UTC, bauss wrote:
> Here is a working example:

That doesn't work with overloads, but yeah, it isn't too hard to get a 90% solution in a few lines of library code.

> But yeah it would be nice if we could have it built-in, which I don't think has been done.

aye.
September 09, 2021
On Thursday, 9 September 2021 at 14:37:04 UTC, Adam D Ruppe wrote:
> On Thursday, 9 September 2021 at 08:39:05 UTC, bauss wrote:
>> But yeah it would be nice if we could have it built-in, which I don't think has been done.
>
> aye.

aye.
September 09, 2021

On Thursday, 9 September 2021 at 14:37:04 UTC, Adam D Ruppe wrote:

>

On Thursday, 9 September 2021 at 08:39:05 UTC, bauss wrote:

>

Here is a working example:

That doesn't work with overloads, but yeah, it isn't too hard to get a 90% solution in a few lines of library code.

>

But yeah it would be nice if we could have it built-in, which I don't think has been done.

aye.

Think it would be nice to extend it so that we can get template arguments as well?

If we're thinking if implementing this thing, might as well go the full way

September 09, 2021
On Thursday, 9 September 2021 at 15:43:42 UTC, Tejas wrote:
> Think it would be nice to extend it so that we can get template arguments as well?

well those would have to be separate, but yeah, i'd be ok with a template argument as well.

less useful to me in general but a harmless add at least.
September 13, 2021

On Wednesday, 8 September 2021 at 21:53:21 UTC, Per Nordlöw wrote:

>

Adam, and like others including me, are longing for a trait/builtin [1] that returns the parameters passed to the function of the current scope (if any). Has such a thing been proposed previously?

[1] http://dpldocs.info/this-week-in-d/Blog.Posted_2021_07_26.html#on-my-wish-list

Implemented https://github.com/dlang/dmd/pull/13071

September 13, 2021

On Monday, 13 September 2021 at 11:43:10 UTC, max haughton wrote:

>

On Wednesday, 8 September 2021 at 21:53:21 UTC, Per Nordlöw wrote:

>

Adam, and like others including me, are longing for a trait/builtin [1] that returns the parameters passed to the function of the current scope (if any). Has such a thing been proposed previously?

[1] http://dpldocs.info/this-week-in-d/Blog.Posted_2021_07_26.html#on-my-wish-list

Implemented https://github.com/dlang/dmd/pull/13071

That is beautiful.

It looks so much better with compiler support!

1 2
Next ›   Last »