FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Anonymous Classes!?

 
Post new topic   Reply to topic     Forum Index -> Tutorials
View previous topic :: View next topic  
Author Message
antdengineer



Joined: 06 Jul 2006
Posts: 10

PostPosted: Wed Aug 02, 2006 5:02 pm    Post subject: Anonymous Classes!? Reply with quote

Hello,

Can anyone here give an example of declaring anonymous classes in d. I cant seem to get it right. Whats the syntax?

Thanks,

Anthony
Back to top
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Thu Aug 03, 2006 12:51 pm    Post subject: Reply with quote

By my understanding, the following should work:

Code:

import std.stdio : writefln ;

class Foo {
  public int[] data;

  static interface IApply {
    int opApply (int delegate (inout size_t, inout int)) ;
  }

  int opApply (int delegate (inout size_t, inout int) dg) {
    int result = 0;

    foreach (i, inout x; data) {
      result = dg(i, x);
      if (result)
        break;
    }
    return result;
  }

  Foo.IApply reverse () {
    return new class Foo.IApply {
      int opApply (int delegate (inout size_t, inout int) dg) {
        int result = 0;

        for (size_t i = data.length - 1; i >= 0; i--) {
          result = dg(i, data[i]);
          if  (result)
            break;
        }
        return result;
      }
    };
  }

}

void main () {
  Foo f = new Foo;

  f.data ~= 27;
  f.data ~= 42;
  f.data ~= 99;

  foreach (i, x; f) {
    writefln("?d -> ?d", i, x);
  }

  foreach (i, x; f.reverse) {
    writefln("?d -> ?d", i, x);
  }
}


But, when attempting to compile it, I got the following odd error:
Quote:
OPTLINK (R) for Win32 Release 7.50B1
Copyright (C) Digital Mars 1989 - 2001 All Rights Reserved

anon0.obj(anon0)
Error 42: Symbol Undefined _D5anon03Foo6IApply7opApplyFDFKkKiZiZi
--- errorlevel 1

_________________
Chris Nicholson-Sauls
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
pragma



Joined: 28 May 2004
Posts: 607
Location: Washington, DC

PostPosted: Thu Aug 03, 2006 1:10 pm    Post subject: Reply with quote

I got the same result:

Code:

anon.obj(anon)
 Error 42: Symbol Undefined _D4anon3Foo6IApply7opApplyFDFKkKiZiZi
--- errorlevel 1


DDLInfo to the rescue!

Here is a dump of all the symbols generated by this sourcecode sample. FYI, the module name is 'anon'.

Code:
strong _D4anon3Foo7opApplyFDFKkKiZiZi
strong _D4anon3Foo7reverseFZC4anon3Foo6IApply
strong _D4anon3Foo7reverseFZC4anon3Foo6IApply12__anonclass17opApplyFDFKkKiZiZi
strong __Dmain
strong _Dmain14__foreachbody2FKkKiZi
strong _Dmain14__foreachbody3FKkKiZi
strong __arguments_Aaki
strong _array_4anon
extern _main
extern __acrtused_con
weak __ModuleInfo_3std5stdio
extern __nullext
extern __d_array_bounds
extern __init_10TypeInfo_i
extern __init_10TypeInfo_k
extern __init_11TypeInfo_Aa
extern _D3std5stdio8writeflnFYv
extern _D4anon3Foo6IApply7opApplyFDFKkKiZiZi
extern __d_arrayappendc
extern __d_newclass
extern _D6object6Object8opEqualsFC6ObjectZi
extern _D6object6Object5opCmpFC6ObjectZi
extern _D6object6Object6toHashFZk
extern _D6object6Object8toStringFZAa
extern _D6object6Object5printFZv
extern __Class_6Object
extern __vtbl_9ClassInfo
extern _D9invariant12_d_invariantFC6ObjectZv
strong __Interface_4anon3Foo6IApply
strong __init__D4anon3Foo7reverseFZC4anon3Foo6IApply12__anonclass1
strong __Class__D4anon3Foo7reverseFZC4anon3Foo6IApply12__anonclass1
strong __vtbl__D4anon3Foo7reverseFZC4anon3Foo6IApply12__anonclass1
strong __init_4anon3Foo
strong __Class_4anon3Foo
strong __vtbl_4anon3Foo
strong __ModuleInfo_4anon


Technically the linker is correct: it's not defined. What's wrong is that it generates an external reference to Foo.IApply.opApply as though it's a method that belongs in another module. I'd expect to see it referenced as vtable offset or somesuch.

If you can whittle this down to a minimal case, I'd strongly reccomend posting a bug report on the matter. This is most definately a subtle codegen bug of some kind.
_________________
-- !Eric.t.Anderton at gmail
Back to top
View user's profile Send private message Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Tutorials All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group