Orhan Tozan
03/23/2020, 12:12 PMdata class ChatConversation
, where a chat conversation is always between two users (userId), which type would fit the most? I was thinking about a val userIds: Set<Int>
but that doesn't enforce the min 2, max 2. Also thought about a val userIds: Pair<Int, Int>
, but the fact that Pair(1, 2) == Pair(2, 1)
equals to false makes it a inproper fit given the case here, since a conversation between Bob and Alice should be considered the same as a conversation between Alice and Bob.
EDIT: Possible option:
data class TwoSizedSet<T>(val value: Set<T>) {
init {
require(value.size == 2)
}
}
cedric
03/23/2020, 5:08 PMOrhan Tozan
03/23/2020, 5:10 PMwhere userA = 1 and userB = 2
, it forces me to check on both sides (no difference between a conversation of Bob and Alice, and Alice and Bob)cedric
03/23/2020, 5:13 PMdata class TwoSize<T>(val first: T, val second: T)
Orhan Tozan
03/23/2020, 5:14 PMPair<T>
?cedric
03/23/2020, 5:14 PMequal
semantic of the class I just wrote will be similar to Pair
so you probably don’t want thatSet
is definitely not the right choice though 🙂Orhan Tozan
03/23/2020, 5:15 PMcedric
03/23/2020, 5:15 PMthe fact thatequals to false makes it a inproper fit given the case here, since a conversation between Bob and Alice should be considered the same as a conversation between Alice and Bob.Pair(1, 2) == Pair(2, 1)
Orhan Tozan
03/23/2020, 5:18 PMPair(1, 2) == Pair(2, 1)
equals to true, which isn't the case. That makes the requirement unordered, instead of ordered. Right?cedric
03/23/2020, 5:20 PMOrhan Tozan
03/23/2020, 5:21 PM