UFCS on forward reference

Jens Mueller jens.k.mueller at gmx.de
Tue May 15 00:35:40 PDT 2012


Ali Çehreli wrote:
> On 05/14/2012 10:02 PM, Timon Gehr wrote:
> > On 05/15/2012 04:28 AM, John Belmonte wrote:
> >> C API's often use a opaque struct pointer as a handle. Mapping such a
> >> struct to D using a forward declaration, I noticed that UFCS doesn't
> >> work:
> >>
> >> struct State;
> >> ...
> >> State* s = new_state();
> >> foo(s); // ok
> >> s.foo(); // compile error
> >>
> >> Error detail:
> >>
> >> Error: struct State is forward referenced when looking for 'foo'
> >> Error: struct State is forward referenced when looking for 'opDot'
> >> Error: struct State is forward referenced when looking for 'opDispatch'
> >>
> >> I'm wondering if anything would be harmed by removing this restriction.
> >>
> >> As a workaround I can use "struct State {}", but that feels wrong.
> >>
> >
> > This is a compiler bug. You can report it here:
> > http://d.puremagic.com/issues/
> 
> I would expect the compiler to need to see the definition of S to
> know that it really does not have a matching foo() member function.

Yes. I think Ali is right. Because UFCS only happens if State has no
member foo. Hence, this is not a bug.
Why don't you import the module where State is defined? I mean if you
want to call a member function on a struct the struct's definition must
be known to statically check if that member function is declared.
Otherwise you have to resort to C style foo(s) because here nothing
needs to be known statically besides the free function foo() accepting a
State* and State must be forward declared.

Jens


More information about the Digitalmars-d mailing list