Thread overview
New syntax for bindings to C++ and namespaces - question
Nov 03, 2018
Cecil Ward
Nov 03, 2018
Paul Backus
Nov 03, 2018
Jonathan M Davis
November 03, 2018
Considering the syntax

    extern( C++, "myCppNamespace" ) void cppFunc();

If you want to still be able to use the namespace-qualified form myCppNamespace.cppFunc() in your D, even with the string type of reference, what about something like

    extern( C++, "myCppNamespace" alias myCppNamespaceAlias ) void cppFunc();

which declares that the name myCppNamespaceAlias is an alias in D for the C++ namespace myCppNamespace, and so would then permit the use of myCppNamespaceAlias.cppFunc().

Would such a thing even be a worthwhile enhancement?

That could get very repetitive though, so it would be better to be able to declare an association between the C++ namespace string and its alias just once. There are many many possible ways that might be done. Perhaps something such as :

    alias myCppNamespaceAlias = extern( C++, "myCppNamespace" );
    /*either */ extern( C++, "myCppNamespace" ) void cppFunc();
    /* or */      extern( C++, myCppNamespaceAlias ) void cppFunc();

Or merely:

    extern( C++, "myCppNamespace" alias myCppNamespaceAlias );
    /*either */ extern( C++, "myCppNamespace" ) void cppFunc();
    /* or */      extern( C++, myCppNamespaceAlias ) void cppFunc();

Any thoughts?

Would such a thing even be a worthwhile enhancement?


November 03, 2018
On Saturday, 3 November 2018 at 05:49:53 UTC, Cecil Ward wrote:
> Considering the syntax
>
>     extern( C++, "myCppNamespace" ) void cppFunc();
>
> If you want to still be able to use the namespace-qualified form myCppNamespace.cppFunc() in your D, even with the string type of reference, what about something like
>
>     extern( C++, "myCppNamespace" alias myCppNamespaceAlias ) void cppFunc();

Use a separate module:

--- myCppNamespace.d
module myCppNamespace;

extern(C++, "myCppNamespace") void cppFunc();

--- main.d
static import myCppNamespace;

void main()
{
    myCppNamespace.cppFunc();
}
November 03, 2018
On Saturday, November 3, 2018 12:02:19 AM MDT Paul Backus via Digitalmars-d wrote:
> On Saturday, 3 November 2018 at 05:49:53 UTC, Cecil Ward wrote:
> > Considering the syntax
> >
> >     extern( C++, "myCppNamespace" ) void cppFunc();
> >
> > If you want to still be able to use the namespace-qualified form myCppNamespace.cppFunc() in your D, even with the string type of reference, what about something like
> >
> >     extern( C++, "myCppNamespace" alias myCppNamespaceAlias )
> >
> > void cppFunc();
>
> Use a separate module:
>
> --- myCppNamespace.d
> module myCppNamespace;
>
> extern(C++, "myCppNamespace") void cppFunc();
>
> --- main.d
> static import myCppNamespace;
>
> void main()
> {
>      myCppNamespace.cppFunc();
> }

Yeah, pretty much the entire point of the new syntax was to make it so that extern(C++) was _only_ about name mangling, just like extern(C) is only about name mangling. You can use D's module system to full effect if you want to organize the symbols rather than stick the entire namespace in the same file - or you can just use the old extern(C++) syntax. Basically, with the new extern(C++) syntax, you can organize C++ symbols like you would D symbols, and that includes using stuff like static imports if that's what you prefer.

- Jonathan M Davis