Accessing class with module name as Java's

Colin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jan 13 01:22:03 PST 2015


Have the following directory structure?

~/testModule$ find . -print
.
./net
./net/http_.d    # **
./net/http
./net/http/Mod1.d
./net/http/Mod2.d
./main.d

** I put the _ here to make it seperate from the net/http
directory. Probably, a better solution exists, this was hacked 
together quickly :)

In http_.d have:
module net.http_;  // again, a better solution surely exists
public import net.http.Mod1;
public import net.http.Mod2;

Then in your main have
import net.http_;

You can then use whatever's in Mod1 and Mod2 modules.
In your case, Mod1/Mod2 will be like
HTTPConnectionRequest/HTTPConnectionResponse and will only
contain a single class. This works for me.

You can also go a little further and have renamed imports.
Something like:
public import Renamed = net.http.Mod2;

Then in main.d call it with:
Renamed.Mod2 mod2 = new renamed.Mod2;

Feels just like Java :)


More information about the Digitalmars-d-learn mailing list