https://kotlinlang.org logo
p

pablisco

01/09/2020, 11:28 PM
What’s the rationale behind not allowing sealed classes on multiple files? One use case would be for Android’s RecyclerView and its Viewholder. If there are a significant number of cases it may get a bit too long to keep them all on the same file but on the other hand sealed classes are ideal for this kind of type where the number of implementations is known at compile time.
b

bogdoll

01/10/2020, 4:19 PM
How would you be sure to have found all classes? Having them in one file it is ensured that only those classes are the full extend of implementation and such an exhaustive switch can be guaranteed.
a

alexsullivan114

01/10/2020, 7:44 PM
Couldn't you just search the entire project for all subtypes? I don't understand why the compiler would care that they're in one file. That being said, having multiple sealed classes in multiple files would 1. make conceptualizing ADTs waaaaaaaaaaay harder since you have to look in any number of places and 2. probably make compilation much slower? Not sure about that point but I imagine having to look through the entire source code of the project would take a lot more time.
p

pablisco

01/10/2020, 10:39 PM
Now I think of it, it may be tricky to block other JVM languages from extending the base type 🤔 I wouldn't recommend it for simple ADTs bit there are certain use cases that could benefit from the split. That said, in many cases we can be reduced in size by extracting behaviour with ext funs and the like. I was just wondering about any technical or design limitations, that's all 😁
👍 1
e

elizarov

01/13/2020, 9:53 AM
4 Views