Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 3309 parameter names trait #951

Closed
wants to merge 3 commits into from

Conversation

jmaschme
Copy link

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

Added a new traits call to get the parameter names of a function
@jmaschme
Copy link
Author

@JakobOvrum
Copy link
Member

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.

else if (ident == Id::parameterNames)
{
FuncDeclaration *f;
Dsymbol *s = getDsymbol((Object *)args->data[0]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@mrmonday
Copy link
Contributor

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
Copy link
Contributor

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

@jmaschme
Copy link
Author

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
Copy link
Member

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
Copy link
Contributor

@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
Copy link
Member

@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
Copy link
Contributor

@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
Copy link
Member

Done as: 08811d7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants