Extending a class or struct

Alan Smith alanrogersmith at gmail.com
Sat Dec 29 08:30:01 PST 2007


Hello everyone,

I am new to D so try to overlook any obvious mistakes I make, however, do point them out to me,

I want to know if it is possible to extend a class or struct in D, like what can be done with arrays.

import std.stdio;

char lastCharacter(char[] str)
{
   return str[length - 1];
}


void main()
{
   char[] str = "Hello World!";
   
   writefln("last character in '%s' is '%s'", str, str.lastCharacter());
}


I find being able to extend a class very useful. For an example of the type of extending I want to do checkout the following link.

http://en.wikipedia.org/wiki/Objective-C#Example_usage_of_categories

My question is, is that sort of thing possible in D?

Thanks in advance for any help you provide.

Peace, Alan


More information about the Digitalmars-d-learn mailing list