D dropped in favour of C# for PSP emulator

H. S. Teoh hsteoh at quickfur.ath.cx
Fri May 11 11:55:05 PDT 2012


On Fri, May 11, 2012 at 08:38:41PM +0200, Mehrdad wrote:
> On Friday, 11 May 2012 at 18:21:24 UTC, H. S. Teoh wrote:
> >Templates are stencils for generating code. There's nothing
> >confusing about that.
> 
> 
> "Stencils for generating code"? _This_??! :O
> 
>   template hasMember(T, string name)
>   { enum hasMember = __traits(hasMember, T, name); }
> 
> 
> Imagine a new user's confusion when seeing something like this.
> (Not sure I got it exactly right, but my point is there.)

Yes, that's exactly how stencils work. You're essentially generating a
declaration of the form:

	enum hasMember = ...;

where what's in the ... depends on the parameters you passed to the
template. So for example, if T is say MyClass, and name is myfield, then
the stencil stamps out:

	enum hasMember = __traits(hasMember, MyClass, myfield);

Now, bad __traits syntax aside (and I do agree with you that __traits()
could've had more intuitive syntax), this is just a piece of code that
depends on some compile-time knowledge. So if MyClass has a field called
myfield, then the final code produced is just:

	enum hasMember = true;

Otherwise, the code produced is:

	enum hasMember = false;

You could've typed out the final code yourself, and the effect is
exactly the same. The only difference is that capturing the process of
producing this code inside a template allows you to do generic
programming, that is, instead of checking each class for a particular
member in every instance, you define a template that, in effect,
replicates this process for you. And once you have this automation, you
can build upon it -- whence comes templates that call other templates,
function signatures constraints, etc..

If newbies had the proper introduction to the concept of templates
(instead of being dunked in the deep end of the pool with signature
constraints that use templates, nested templates, etc., with no prior
warning), they wouldn't have such a hard time with them.


T

-- 
There are 10 kinds of people in the world: those who can count in binary, and those who can't.


More information about the Digitalmars-d mailing list