Hacker News new | past | comments | ask | show | jobs | submit login

Yeah, this is a problem in a couple modern languages. For some time I tried to make a binding for Nim and encountered the same problem.

How do you make it so you can call "Widget" methods on a "Button"? Without proper subclassing, you have to either create a lot of duplicate functions, or the user has to explicitly cast up. Nim has implicit converters, so I tried defining conversions from all types to their base classes (Button -> Widget). But this made compilation time insanely slow.

Modern languages make this about "favor composition over inheritance" and say it helps you avoid monstrous Java-esqe class hierarchies. But it is really about subtyping and the Liskov substitution principle. You have a heterogeneous list of different "Widgets", how easily can you treat them as one kind, even if they need different behavior in detail?

Nim had experimental support for generics co-/contravariance. So you could say a "IF Button struct is a subclass of Widget struct THEN a GObjectPointer[Button] can be used in all cases where a GObjectPointer[Widget] can be used" (covariance). This was a unique feature and would solve the problem without implementation inheritance, but unfortunately it didn't work properly and I think it has been removed.




Nim actually does support inheritance though, so you could have just used that with methods. Though I can understand if you wanted to avoid inheritance.


That's true. I think my problem was that I was trying to be to clever and trying to make something like SmartPtr[TButton] have the same inheritance like ptr TButton. If I would have "wrapped" everything in native Nim classes it would have been fine. OTOH, I think there is no equivalent to an interface in Nim, so that would not work in that case.

I'm not saying Nim is deficient, in fact it is one of the most expressive and productive languages I know. But there are subtle difference between object models in different languages that make GUI programming hard, and I think that is one reason many people don't use modern languages but stay on C++/C# or throw there hands up in the air and just use Electron...


Yeah, tbh I think lack of interfaces is one of the biggest misses in Nim.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: