Final makes no difference?

BCS BCS at pathlink.com
Thu Aug 24 13:28:26 PDT 2006


Chris Nicholson-Sauls wrote:
> BCS wrote:
[...]
>> interface Foo {
>>     char fig(int);
>>     int bar(char);
>> }
>> void fun()
>> {
>>     char[] cmap;    int[] imap;
 >>
>>     char first(int i){return cmap[i];}
>>     int second(char c){return imap[c];}
>>
>>     Foo f = interface : Foo {   // making up a syntax...
>>             alias first fig;
>>             alias second bar;
>>         }
>>     FnTakingFoo(f);
>> }



Neat work-around!

It does have the problem of double indirection in there:

f.classContex.functionContext[offset_s]
vs.
f.functionContext[offset_s]

But that's not that bad an issue.

Having the alias trick fail is more of an issue, but still not a big 
one. (BTW I expect this fails because the aliased function ends up being 
static or some such problem)

Also it is still a workaround that subverts something into something 
else it wasn't intended for.

My main question why that syntax even works? Shouldn't there be a ":" 
between the "class" and the "Foo"?

> 
> 
> 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.
> 
> -- Chris Nicholson-Sauls



More information about the Digitalmars-d-bugs mailing list