Final makes no difference?

Bruno Medeiros brunodomedeiros+spam at com.gmail
Fri Aug 25 07:43:17 PDT 2006


Sean Kelly wrote:
> Chris Nicholson-Sauls wrote:
>>
>> Tested with DMD 0.165, no errors, runs perfectly:
>>
>> # module funcface0;
>> #
>> # import std .stdio ;
>> #
>> # interface Foo {
>> #   char[] str () ;
>> #   int    num () ;
>> # }
>> #
>> # void bar (Foo f) {
>> #   writefln("str('%s')  num(%d)", f.str(), f.num());
>> # }
>> #
>> # void func (char[] s, int n) {
>> #   Foo f = new class Foo {
>> #     char[] str () { return s ; }
>> #     int    num () { return n ; }
>> #   };
>> #   bar(f);
>> # }
>> #
>> # void main () {
>> #   func("The answer is", 42);
>> # }
>>
>> I did try your trick of aliasing nested functions into the anonymous 
>> class' scope... Unfortunatley it didn't work; DMD complained the 
>> interface was left unimplemented.  Not a really big deal, as anonymous 
>> classes apparently get access to the frame they were created in, the 
>> same as a delegate does I'd imagine.
> 
> That compiles?  How weird.  I'd expect the assignment to "Foo f" to fail 
> with a "cannot convert class Foo to interface Foo" type error.  Does DMD 
> simply infer the relationship from the assignment?
> 
> 
> Sean


There is no class Foo. The class literal is an anonymous class which 
implements the Foo interface. That's what "new class Foo {..." means.
(http://www.digitalmars.com/d/class.html - Anonymous Nested Classes)

-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d-bugs mailing list