-
|
How can this be done? luasocket's udp has two states/types: local udp = socket.udp() --unconnected
udp:setpeername("10.0.0.1", port) --now the socket is connected!
-- We can use functions from connected sockets
udp:send("packet")
-- But can't use functions from unconnected sockets
udp:sendto("packet", "10.0.0.2", 12345) --Error "expected unconnected socket, got {connected}"The problem lies in: What type should the udp socket be in? Can I change it's type when calling Even if the latter was possible, I would still need to be able to check that: if the first argument for The only way I can think of fixing it would be to do: ---@type UDPSocketUnconnected | UDPSocketConnected
local udp = socket.udp()But it then won't know when certain functions can be called. Like in the example above, where |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
Unfortunately I have no experience with Perhaps |
Beta Was this translation helpful? Give feedback.
I think you hit the nail. It might just not be possible.
I've ultimately decided to return both
(UDPSocketUnconnected | UDPSocketConnected). Unfortunately some functions are dependent on whether its connected or not, so I can't just return a generic.Take a look at LuaCATS/luasocket#6.