I have a usecase where I send some data to my server (as analytics) which is always either String, Boolean or Number.
How can I enforce the caller to send only a number, boolean or string and NOT any other objects?
Following cases should work-
userProperties: MutableMap<String, in AnyPrimitive> = mutableMapOf(),
userProperties.put("someKey", 1)
userProperties.put("someKey", 1.2f)
userProperties.put("someKey", "someValue")
userProperties.put("someKey", true)
but not
userProperties.put("someKey", myCustomObjectInstance)
Approach I tried was creating an abstract class EventData which implements CharSequence and Number. But this requires everyone to create an instance of these class and not send simply a number or a string.
I can put a logic to check the type and throw exception but I would prefer restricting it at compile time.