I have a class that takes a list of `Any` but the ...
# announcements
p
I have a class that takes a list of
Any
but the subtypes are not directly related so they can't be sealed classes.
a
You can create a sealed wrapping type:
Copy code
sealed class Wrapper{
   class TypeA(val value: A): Wrapper()
   class TypeB(val value: B): Wrapper()
   ...
}
Then accept
List<Wrapper>
and get the exhaustive
when
you probably are after.
Obviously there's probably a better name than
Wrapper
but I don't know your domain.
p
That's too performance intensive unfortunatelly
If it were inline classes... 😉
a
One allocation per item is not going to break the bank! If you have a very large list already, then you likely already have these performance issues.
p
No, but it doubles what's in the bank ^^
a
how so? It doubles the number of objects referred to by the list. But that doesn't mean double the memory, or double the processing time. What are we talking here, 100 items, 1000?
I guarantee you that if you profile your application, you will see far lower hanging fruit that worrying about a few object allocations. Profile, and optimize the inner loops, I would not optimize upfront like this, it is always a waste of time.
👍 1