Get the name of a function and the parameters?

Don nospam at nospam.com
Thu Apr 30 07:21:36 PDT 2009


Georg Wrede wrote:
> Jarrett Billingsley wrote:
>> On Wed, Apr 29, 2009 at 2:52 AM, Georg Wrede <georg.wrede at iki.fi> wrote:
>>> Jarrett Billingsley wrote:
>>>
>>>> Don't you love it?  "Most C++ template features are discovered."  So 
>>>> are
>>>> D's.
>>> Well, one could say that this is the very definition of a well working
>>> metaprogramming system. After all, the whole idea of templates is to 
>>> let the
>>> programmer invent new ways to use the lanaguage, the compiler, and the
>>> template system.
>>
>> That's not.. really at all what the issue is here.  The issue is more
>> that most of the metaprogramming features in D are completely
>> unspecified, and when we discover that something works, it's hard to
>> tell if it's _supposed_ to be that way or if we're taking advantage of
>> some weird bug in the compiler.  Show me a page that documents
>> .stringof!  Thought not.
> 
> Yeah. Just today I posted to Bugzilla asking for an easy way to get the 
> current function name and parameter names. And yes, once you find that 
> you can get some info or get something done with some gymnastics, you 
> can't rely on it being there tomorrow. For all you know, your whole 
> solution may be based on a computer bug that'll get fixed next week.
> 
>> As some other examples, did you know it's possible to get the names of
>> the fields of a struct type (and probably a class type too with a
>> similar method)?  It only works under extremely contrived
>> circumstances, using .stringof, and parsing out the names yourself
>> from a very horrible-looking string.  It's also possible to get the
>> name of a local variable by instantiating a template in a local scope
>> and parsing a horrid .mangleof string.  You can also determine some
>> (not *all*, but some) interesting properties of functions and methods
>> - things like final, static, abstract - by creating dummy inherited
>> classes and attempting to do awful things to them.
> 
> Sigh. The good thing is, you actually /can/ get the stuff. And it's 
> frustrating. I mean, folks do stumble on these ways of getting the 
> impossible, but the real crappy thing is if you know what you want, it's 
> pretty hard to then come up with the jumps and somersaults needed.
> 
>> Metaprogramming should be _well-specified_ and _orthogonal_.  I agree
>> with you that the _possibilities_ of metaprogramming should be almost
>> boundless and should allow you to come up with your own DSLs.  But the
>> compiler shouldn't hold information about your program "ransom" and
>> make you work so goddamn hard to get at it.  I should be able to just
>> write, I don't know, __traits(parameterNames, f) and get a tuple of
>> parameter names!  I'm tired of puzzling out information that should be
>> directly available!
> 
> Somehow, I can't help believing that will be the case, already before D2 
> gets out. It's unimaginable that Andrei &co wouldn't have this as the 
> goal. And with their current speed, that day is pretty near!

Another factor -- we all have the compilable DMD backend now. With many 
of these things, we can still make them happen even though Walter's 
overloaded.

> 
> (Ok, ok, in private I do share your frustration. I too do swear att 
> stuff not being documented, others being illogical or plain too hard to 
> use, etc. But I don't know if it serves the end goal if we could have 
> Andrei do more documenting, or whatever. That'd all be time off of the 
> real thing.)
> 
> -----
> 
> In the mean time, maybe we should all start writing on the "best 
> practices" on wiki4d every time we stumble upon a way to get/do 
> something cool/useful. Pasting from there sure is faster than spending 
> three days each time thinkng and asking around....


More information about the Digitalmars-d-learn mailing list