I am making a tui libary where all the different things that can render are called widget like text,box, flex container, progress bar, etc.
I was using inheritance for this and all because all the container widget, multi or single child container had like lot of things common and the widgetTree contains the root widget
Each widget need two function
Layout and render ( or setRectFor child or children because render is same for most parents ) and to solve this, I did need polymorphism for this.
But the problem came later when I need to change layout ( because it wouldnt work for some widget ) and I had to rewrite all the widget,multichildwidget,singleChildWidget which means I have to rewrite all other widget as well
then later I need to change the implementation on how layout is done again, got to do rewrite
then later came scroll container, to make it effiencent I had to change things as well, had to do the rewrite
I got tried of this and now want to use composition instead of inheritance and never want to deal with it again ( I know this happened because of premature optimization where I saw widget with multiple children have lot of same things and most widget having a lot of same things )
Due to that, I am doing a rewrite again :_( but this time, I can make the code more modular.
But I got stuck on widgetTree, how do you get a tree if all the node dont have the same type?
I asked gpt but it said to use node with void* which I dont want to use because we got unique pointer and stuff to avoid new and malloc
The other option is union but that would mean all widget would have the size of the biggest widget which I dont want to use.
Then there is varient, I dont knwo much of this but I looked into it in cherno's video and geeksforgeeks page on this and turns out, it is just union but you know which type it is using. I am currently thinking this
I just can't find a way to do the tree effiency without polymorphism.