std.socket classes

Jonathan Marler via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Apr 9 07:47:39 PDT 2017


Does anyone know why Socket and Address in phobos were created as 
classes instead of structs?

My guess is that making Socket a class prevents socket handle 
leaks because you can clean up the handle in the destructor when 
the memory gets freed if no one closes it.  Is this the reason it 
is a class and are there any other reasons?

As for Address, I can't think of a reason why this one is a 
class.  It doesn't have to free any underlying OS resources, it's 
just a chunk of memory that can be passed to and from the socket 
API.  Using sockaddr in C/C++ is more flexible because it allows 
the application to decide where the memory lives (which will 
almost always be on the stack).  It feels like whoever made 
Address a class probably wasn't familiar with sockaddr.  Is that 
the case or are there reasons why it was made a class?

If I was implementing sockaddr in D, I would have chosen to use 
addressFamily as a sort of "makeshift Vptr", which is really how 
it is used in C (even though C doesn't support classes). Using 
this technique, I believe you could expose pretty much the same 
API without the overhead of wrapping it inside a D object.  Does 
anyone know if this solution was considered?


More information about the Digitalmars-d-learn mailing list