April 28, 2009
On Tue, Apr 28, 2009 at 3:07 PM, grauzone <none@example.net> wrote:
>
> I'd like to pass several functions at once. Is there a way to make this
> variadic? The obvious approach (writing "NameOfFunc(alias f...)") fails with
> a syntax error.

Sure, you'd just make it NameOfFunc(f...) and then recursively instantiate, converting one item at a time to its name until f.length == 0.

Alternatively, you can write a compile-time map and use NameOfFunc as the mapping predicate.
April 29, 2009

Jarrett Billingsley wrote:
> On Tue, Apr 28, 2009 at 3:07 PM, grauzone <none@example.net> wrote:
>> I'd like to pass several functions at once. Is there a way to make this
>> variadic? The obvious approach (writing "NameOfFunc(alias f...)") fails with
>> a syntax error.
> 
> Sure, you'd just make it NameOfFunc(f...) and then recursively instantiate, converting one item at a time to its name until f.length == 0.
> 
> Alternatively, you can write a compile-time map and use NameOfFunc as the mapping predicate.

That requires f to be a type, which loses you the actual names.  And you cannot (last time I checked) have aliases in a tuple.

  -- Daniel
April 29, 2009
On Tue, Apr 28, 2009 at 10:23 PM, Daniel Keep <daniel.keep.lists@gmail.com> wrote:

> That requires f to be a type, which loses you the actual names.  And you cannot (last time I checked) have aliases in a tuple.

Check again - tuples can be any arbitrary mix of types, expressions, and aliases.  :)
April 29, 2009
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.

The worst possible template system is one where everything is by design, and nobody ever discovers anything. One would then ask what the whole point of the template system is.
April 29, 2009
Jarrett Billingsley wrote:
> On Tue, Apr 28, 2009 at 10:23 PM, Daniel Keep
> <daniel.keep.lists@gmail.com> wrote:
> 
>> That requires f to be a type, which loses you the actual names.  And you
>> cannot (last time I checked) have aliases in a tuple.
> 
> Check again - tuples can be any arbitrary mix of types, expressions,
> and aliases.  :)

Wow, it actually works. But I really don't understand why. It seems the compiler directly passes symbols as tuple arguments, and later dumbs it down to what it needs (like an alias, a type, ...).

Makes me think nobody will ever be able to write a separate bug-free DMD frontend. I'm scared.
April 29, 2009
On Wed, Apr 29, 2009 at 3:03 AM, grauzone <none@example.net> wrote:

> Wow, it actually works. But I really don't understand why. It seems the compiler directly passes symbols as tuple arguments, and later dumbs it down to what it needs (like an alias, a type, ...).
>
> Makes me think nobody will ever be able to write a separate bug-free DMD frontend. I'm scared.

Precisely.  There is no principle of least surprise in the DMD metaprogramming implementation.  There is no consistency.  And worst of all, there is no specification.
April 29, 2009
On Wed, Apr 29, 2009 at 2:52 AM, Georg Wrede <georg.wrede@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.

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.

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!
April 29, 2009
Jarrett Billingsley wrote:
> On Wed, Apr 29, 2009 at 3:03 AM, grauzone <none@example.net> wrote:
> 
>> Wow, it actually works. But I really don't understand why. It seems the
>> compiler directly passes symbols as tuple arguments, and later dumbs it down
>> to what it needs (like an alias, a type, ...).
>>
>> Makes me think nobody will ever be able to write a separate bug-free DMD
>> frontend. I'm scared.
> 
> Precisely.  There is no principle of least surprise in the DMD
> metaprogramming implementation.  There is no consistency.  And worst
> of all, there is no specification.

And that tears down the phrase "The lexer, parser and semantic pass don't know each other so that it's easier to implement a compiler for D". Because a D compiler that doesn't use DMD's front-end is not D compatible. Because the front end *is* the specification of the language.
April 29, 2009
> D". Because a D compiler that doesn't use DMD's front-end is not D compatible. Because the front end *is* the specification of the language.

...and it's full of bugs.
April 29, 2009
Jarrett Billingsley wrote:
> On Wed, Apr 29, 2009 at 2:52 AM, Georg Wrede <georg.wrede@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!

(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....