For Chuck Allison: possible homework in D

Chris via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 9 03:32:09 PDT 2014


On Monday, 9 June 2014 at 10:01:25 UTC, Philippe Sigaud via 
Digitalmars-d wrote:
>> struct / class Element(T) {
>>   T name;
>>   T[T] attributes;
>>   // ...
>>   string toString() {
>>     return ...;
>>   }
>> }
>
> Why did you chose the same type for keys and values? And 
> shouldn't
> 'name' always be a string?

For a HTML Element it should always be a string (e.g. tagname = 
"p"). However, it could also be of type wchar[], dchar[], char[]. 
For HTML Elements I usually have T[T] (string[string]), but 
you're right, in other contexts it could be size_t[string], 
string[int] etc. I only use it for a web server app. But to make 
it more flexible is not too complicated. If I used it in a truly 
generic context (DOM, latex and whatnot), I would design the type 
system more carefully. But that would have been a waste of time, 
since it's primary application is to return a html string. There 
were more urgent issues.


More information about the Digitalmars-d mailing list