On Monday, March 17, 2025 4:14:21 PM MDT Andy Valencia via Digitalmars-d-learn wrote: > The presence of the "accepting" API in Socket seems to indicate > that subclassing Socket/TcpSocket is intended to be supported. > But I'm just not seeing my way through the twisty maze of pure > and @nothrow barriers? > > Andy > > import std.socket : TcpSocket, Address, getAddress; > > class WrappedTCP : TcpSocket { > string label; > > this(string _label) nothrow { > this.label = _label; > super(); > } > override WrappedTCP accepting() nothrow { > return new WrappedTCP(this.label); > } > } > void main() { > import std.stdio : writeln; > > auto s = new WrappedTCP("My Label"); > writeln(s.label); > s.listen(4); > auto s2 = cast(WrappedTCP)(s.accept()); > writeln(s2.label); > }
The base class constructors are not nothrow, so WrappedTCP's constructor cannot be nothrow. There really isn't a way out of that, because if a constructor throws, the object's state is destroyed. So, catching and handling the Exception to make your function nothrow isn't really an option like it would be with many functions. accepting is pure in the base class, so your implementation must be pure. If you can't do that, then you're out of luck. And given what you're trying to do, that means that your constructor must be pure, and looking at TcpSocket, that's not possible, because none of its constructors are pure. If you gave up on deriving from TcpSocket and derived from Socket instead, you might be able to get it to work. TcpSocket doesn't have _any_ attributes on its constructors, so nothing that requires attributes is going to work with them - including accepting. Really, attributes are _not_ used well in std.socket, and it's clearly problematic for what you're trying to do. But std.socket is quite old and honestly isn't very good, so it doesn't surprise me particularly if you're having issues with it. So, you can try subclassing Socket and making that work, but I don't know how easy that would be. I think that the classes in std.socket were intended to be extensible, but what's there right now isn't very good. It's a module that came from D1, so it predates all of the attributes, and the way that the attributes were tacked on later is clearly bad. So, if anything, it needs a complete redesign or replacement - which will happen in Phobos v3, but that's quite a ways off. So, if you can't make std.socket work the way that you need, you'll either need to find a solution on code.dlang.org or make your own using the underlying C calls. - Jonathan M Davis