logoalt Hacker News

sendfoods • today at 7:13 PM • 3 replies • view on HN

Wouldn’t peer: Option<int32> be enough to represent this?

None = disconnected


Replies

mckn1ght • today at 7:40 PM

Personally I don’t like using nil to represent specific states in my domain. I try to only judiciously use it to represent the lack of knowledge about some state, like if I need to reach out to some external service I don’t control, that may or may not give me an answer for my query (and conversely, I don’t like putting “unknown” cases into my enums).

treyd • today at 7:24 PM

Yes, it'd probably be compiled into very machine code. However defining an enum for it puts the intention into the source code more explicitly, so that's considered the more idiomatic way. You'd probably have a `fn peer_id(&self) -> Option<Inet4Addr>` accessor that you'd use in the case where you'd want the Option.

slopinthebag • today at 8:49 PM

it's just a minimal example, in reality you would typically have various other data that exists exclusively in connected/unconnected states