Why D _still_ needs the C preprocessor

Kirk McDonald kirklin.mcdonald at gmail.com
Sun May 6 03:10:59 PDT 2007


nobody at nowhere.nonet wrote:
> Unknown W. Brackets <unknown at simplemachines.org> spewed this unto the Network: 
>> Hmm.... isn't this what the new mixins are for?
>>
>> Example (just to fit in with your example, you'll have to adjust):
>>
>> ------
>> struct OMF_Record
>> {
>> 	int record_type;
>> }
>>
>> struct library
>> {
>> static:
>> 	OMF_Record[] select_records (char[] clause) ()
>> 	{
>> 		OMF_Record[] results;
>>
>> 		OMF_Record rec;
>> 		if (mixin(clause))
>> 			results ~= rec;
>>
>> 		return results;
>> 	}
>> }
>>
>> int main()
>> {
>> 	OMF_Record[] records = library.select_records!("rec.record_type == 1");
>>
>> 	return 0;
>> }
>> -----
>>
>> That's what you want, right?  I mean, the ! isn't too bad is it?
>>
>> -[Unknown]
> 
> Well, that isn't too bad. But why does it work?  I don't see the word
> "template" anywhere, but at this website:
> 
> 	http://www.digitalmars.com/d/mixin.html
> 
> It never says "mixin" without saying "template" first. I was under
> the impression that mixins only operated on templates.
> 

D has two distinct kinds of mixins: template mixins and the newer string 
literal mixins. The keyword index can help disambiguate them:

http://www.prowiki.org/wiki4d/wiki.cgi?LanguageSpecification/KeywordIndex

The spec very deliberately uses "template mixin" to unambiguously refer 
to one particular kind of mixin. The above code uses the other kind.

> I've also never seen the ! away from the word "mixin". What does the
> "!" mean?
> 
> And why does select_records()() get an extra set of parentheses now?

The ! denotes a template instantiation. In the above, select_records is 
a function template. It has one template argument (char[] clause) and no 
function arguments. We can elide the trailing () when calling it because 
of D's property syntax.

The "clause" argument must be a template argument, since string mixins 
can only operate on compile-time strings.

-- 
Kirk McDonald
http://kirkmcdonald.blogspot.com
Pyd: Connecting D and Python
http://pyd.dsource.org


More information about the Digitalmars-d-learn mailing list