www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Anonymous Classes and Delegates

Has anyone used anonymous classes extensively? I can't tell if I'm trying to 
do something that can't be done, or if it exposes a bug in D.

Here's the test code:

    class Person {
        char[] name;

    Person getPerson(char[] name, Person delegate(char[]) d) {
        return d(name);

    int main() {
        Person person = getPerson("Charlie", delegate Person(char[] name) {
            writefln(name); // 'name' here is ok
            return new class Person {
                this() {
                    this.name = name; // 'name' here seems to be garbage of 
length 4203763
        writefln(person.name); // Access violation thrown here
        return 0;

If I put the anonymous class code in the getPerson method instead of calling 
the delegate, it works. But that isn't what I want. So, can anyone tell me 
why "name" isn't valid in the anoymous class constructor, but is ok in the 
line above it?

Sep 15 2005