Thread overview
Extending a class or struct
Dec 29, 2007
Alan Smith
Dec 29, 2007
Alan Smith
Dec 29, 2007
Christopher Wright
December 29, 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
December 29, 2007
"Alan Smith" <alanrogersmith@gmail.com> wrote in message news:fl5sm9$2srk$1@digitalmars.com...
> 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.

It's something that's coming in D2.  Basically,

a.f(b)

and

f(a, b)

will be (generally) interchangeable for all types.  This way you can "add" methods to classes and structs (and even basic types, like ints and floats) by having the class or struct as the first parameter to the function, much like what you do with arrays now.

This is, however, just syntactic sugar; such "methods" are not really part of those types, and cannot participate in polymorphism.  Which, for anything but classes, doesn't really matter anyway :)


December 29, 2007
Hello Jarret,

So that means I won't be able to use it until... I thought D2 was already out? I'm using GDC because DMD doesn't work on PowerPC, as far as I know. When will this feature be available to me?

THanks, Alan
December 29, 2007
"Alan Smith" <alanrogersmith@gmail.com> wrote in message news:fl64hm$h8g$1@digitalmars.com...
> Hello Jarret,
>
> So that means I won't be able to use it until... I thought D2 was already out? I'm using GDC because DMD doesn't work on PowerPC, as far as I know. When will this feature be available to me?
>
> THanks, Alan

D2 is in alpha and is still in development.  Many of the things promised for it have yet to be worked out or implemented.

When will this be available to you?  When it's developed and when GDC supports the D2 frontend.


December 29, 2007
Alan Smith wrote:
> Hello Jarret,
> 
> So that means I won't be able to use it until... I thought D2 was already out? I'm using GDC because DMD doesn't work on PowerPC, as far as I know. When will this feature be available to me?
> 
> THanks, Alan

D2 has been released. However, it is in active development, with new features being added. Uniform call syntax is one of those features that will be added, and that will happen whenever Walter manages to implement it.

For gdc, well, after the event occurring later: Walter implementing uniform call syntax or gdc supporting D2.