ModularM
Modular16mo ago
38 replies
aurelian

why is this return a copy?

# @value
struct UnixSocket:
    var address: Optional[UnixAddress]
    var fd: Int32

    @staticmethod
    fn connect(path: StringSlice) raises -> UnixSocket:
        socket = UnixSocket()
        address = UnixAddress(path)
        connect(socket.fd, address, sizeof[UnixAddress]())
        socket.address = address^ # this would also be a copy, easy to forget the ^
        return socket # 'UnixSocket' is not copyable because it has no '__copyinit__'

    fn accept(self) raises -> UnixSocket:
        if not self.address:
            raise Error("socket address unintialized")
        conn = accept(self.fd, self.address.value(), sizeof[UnixAddress]())
        return UnixSocket(fd=conn) # this one is fine, not sure why
Was this page helpful?