Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix issue 3309 parameter names trait #951

Closed
wants to merge 3 commits into from

6 participants

@jmaschme

Added a new traits call to get the parameter names of a function

@jmaschme jmaschme Fix issue 3309
Added a new traits call to get the parameter names of a function
f7518cd
@JakobOvrum

All traits returning lists were changed to return expression tuples instead of arrays (e.g. allMembers and friends).

I think this should do the same for consistency.

src/traits.c
@@ -206,6 +206,23 @@ Expression *TraitsExp::semantic(Scope *sc)
StringExp *se = new StringExp(loc, s->ident->toChars());
return se->semantic(sc);
}
+ else if (ident == Id::parameterNames)
+ {
+ FuncDeclaration *f;
+ Dsymbol *s = getDsymbol((Object *)args->data[0]);
@yebblies Collaborator

New code should not be using the data member of arrays. Use (*args)[0] instead, it's typed correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@mrmonday

It would be cool if this returned a tuple of symbols rather than strings - the strings can then be acquired from the symbols if needed.

@mrmonday

It would also be useful if default arguments could be acquired from this too.

@jmaschme

Interesting ideas. I went with string names only because that was the biggest piece missing from the introspection capabilities. And of course, there was already a bug report on it with a patch that I used as the starting point.

Perhaps we should be considering a more generalized getParameters trait? We can already get argument types from std.traits. If you add parameterNames, the only thing missing would be default arguments. Is that enough of a reason for a more generalized trait?

@JakobOvrum

It would be cool if this returned a tuple of symbols rather than strings - the strings can then be acquired from the symbols if needed.

Those symbols only exist inside the function itself. How would it work in general? I don't see how it could.

@mrmonday

@JakobOvrum Good point. I'm not sure how best to handle that, perhaps as @jmaschme suggested we could have a getParameters trait that only worked within a function/method?

@JakobOvrum

@mrmonday, aye, I think that would be great, and I know of at least two people who have asked about exactly this kind of thing before on IRC (using the parameters to the current function as an expression tuple). Retrieving the symbol for the current function is also a frequently requested feature (simplifying interfaces for a lot of mixin-style code).

Either way, I want to thank @jmaschme for taking on these important issues. Your effort is greatly appreciated!

@TurkeyMan

@mrmonday Only working within a function/method may not be sufficient.
I believe this would be used extensively to copy parameter lists to generated functions...

void someOtherFunction(int x, float y = 10.0);
void newFunc(bool test, ParameterListOf!someOtherFunction);

In this case, newFunc should be: newFunc(bool test, int x, float y = 10.0), ie, the copied parameters are NOT a tuple argument, they are 1st class parameters, retaining their names and default args.
I need this for generated user facing API's. This allows auto-complete, intellisense, documentation, pop-up helper tooltips, etc to all work as expected.

Note: Syntax in my example is for illustration only. I don't care at all how it's implemented, assuming the result is the same...

@JakobOvrum, Yeah, I've often needed typeof(this_function), or something to that effect.

@WalterBright
Owner

Done as: 08811d7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 14, 2012
  1. @jmaschme

    Fix issue 3309

    jmaschme authored
    Added a new traits call to get the parameter names of a function
  2. @jmaschme
Commits on May 15, 2012
  1. @jmaschme

    Dropped direct use of data

    jmaschme authored
This page is out of date. Refresh to see the latest.
Showing with 18 additions and 0 deletions.
  1. +1 −0  src/idgen.c
  2. +17 −0 src/traits.c
View
1  src/idgen.c
@@ -324,6 +324,7 @@ Msgtable msgtable[] =
{ "hasMember" },
{ "identifier" },
{ "parent" },
+ { "parameterNames" },
{ "getMember" },
{ "getOverloads" },
{ "getVirtualFunctions" },
View
17 src/traits.c
@@ -206,6 +206,23 @@ Expression *TraitsExp::semantic(Scope *sc)
StringExp *se = new StringExp(loc, s->ident->toChars());
return se->semantic(sc);
}
+ else if (ident == Id::parameterNames)
+ {
+ FuncDeclaration *f;
+ Dsymbol *s = getDsymbol((*args)[0]);
+ if ((f = s->isFuncDeclaration()) == NULL)
+ {
+ error("argument %s is not a function", s->toPrettyChars());
+ goto Lfalse;
+ }
+ Expressions *exps = new Expressions();
+ TypeFunction *tf = (TypeFunction *) f->type;
+ if(tf->parameters)
+ for (size_t i = 0; i < tf->parameters->dim; i++)
+ exps->push(new StringExp(loc, ((Parameter*) tf->parameters->data[i])->ident->toChars()));
+ Expression *e = new TupleExp(loc, exps);
+ return e->semantic(sc);
+ }
else if (ident == Id::parent)
{
if (dim != 1)
Something went wrong with that request. Please try again.