www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Extending a class or struct

reply Alan Smith <alanrogersmith gmail.com> writes:
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
Dec 29 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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 :)
Dec 29 2007
parent reply Alan Smith <alanrogersmith gmail.com> writes:
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
Dec 29 2007
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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.
Dec 29 2007
prev sibling parent Christopher Wright <dhasenan gmail.com> writes:
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.
Dec 29 2007