Accessing class with module name as Java's

tcak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 12 22:09:55 PST 2015


On Tuesday, 13 January 2015 at 05:18:46 UTC, Ali Çehreli wrote:
> On 01/12/2015 08:22 PM, tcak wrote:
> >> What namespace? D has modules, unlike C++. In general it's a
> bad idea
> >> to have inside a module a name (like a variable name or
> struct name)
> >> equal to the module name, because it causes confusion.
> >
> > I am confused as well.
> >
> > core.stdc.errno
> >
> > @property int errno() { return getErrno(); }
> > @property int errno(int n) { return setErrno(n); }
>
> errno better worked similar to what it represents ("the" errno) 
> so I am not surprised that it has the same name as its module. 
> It is like how errno.h defines errno in C.
>
> Like bearophile though, I don't understand the namespace issue. 
> Can you explain it with a piece of code please.
>
> Ali

Right now I am building a web server.

Some of class names those are defined in main program are as 
follows:

HttpSocketConnectionRequest
HttpSocketConnection
HttpSocketListenerEvents

These are all in main.d.


Now, because the length of codes of these classes are long, I 
need to put them into separate files to manage them easily.

Look what I need to do: I will create a file called "request.d", 
and then put "HttpSocketConnectionRequest" into it.

Then create "connection.d", and then put "HttpSocketConnection" 
into.


It is just making everything dirty.
request.HttpSocketConnectionRequest
connection.HttpSocketConnection


I just want to create a file called 
"HttpSocketConnectionRequest.d", and put 
"HttpSocketConnectionRequest" into it. This is how it works in 
Java. So, you do not repeat the names again and again. (I have 
given an example in previous posts in this thread.)


I thought maybe I could achieve this by just using what the 
language provides by doing that:

import HttpSocketConnectionRequest: HttpSocketConnectionRequest;

First one is module name, second one is the name of class that is 
defined in it.

This way, I do not have to repeat the names, and codes are much 
cleaner.


But when the project gets complex, I will need to add sub names 
(what I meant with namespace) like:

HttpSocketConnectionRequest.d
==============
module net.http.HttpSocketConnectionRequest;

class HttpSocketConnectionRequest{}


With this, above solution doesn't work unfortunately. You cannot 
do that with that solution:

auto req = new net.http.HttpSocketConnectionRequest();

Because it requires module name still. You still have to do:

auto req = new 
net.http.HttpSocketConnectionRequest.HttpSocketConnectionRequest();


Which has name repeatition again, and as "many" people would 
agree, doesn't seem like any good design at all.

So, I am looking for a nice solution in the language. My idea was 
to bind a class or struct to a module. Like:

module class HttpSocketConnectionRequest{} (This is just an 
example)

Yes, this would prevent accessing content outside of that class 
in the module, but if you have more things already, programmer 
wouldn't be using binding in the first place as well. It doesn't 
affect language at all, and makes the statements much more 
cleaner. Even Phobos can take advantage of this.


More information about the Digitalmars-d-learn mailing list