Hi there, I was wondering how can one create a custom type that inherits all the methods (procs or whatever) from a builtin type, say seq or sets.HashSet without using converters ?
for example in julia (which i have little experience with) they have abstract types such as AbstractArray and AbstractSet for which they implement a set of methods that a concrete type only needs to subtype the abstract type to have access to
struct SquaresVector <: AbstractArray{Int, 1}
count::Int
end
disclaimer: i am a newbie in nim, julia and programming in general.
To expand a bit on Araq's answer: Inheriting from a low-level data structure is almost always a bad idea, especially if the base class and derived class are on different abstraction levels (as an extreme example, the base class is a HashSet and the derived class a Person). If you inherit that way, you end up with lots of methods in the derived class that make no sense there and will possibly cause problems if you call them.
Usually in OOP, the expectation is that you can use an object of the derived class in place of an object of a base class. Famous examples where this goes wrong, even though they look fine at first sight:
In general you'd want to use composition, not inheritance, i.e. "has a", not "is a". A ToDoList isn't a subclass of seq; it's an object that contains a seq as one of its members. You can still use all the seq methods; you're just calling them on todo.items, not todo itself.
As @sschwarzer said, the problem with inheriting from a general-purpose class is that your subclass inherits all those general-purpose characteristics. In the case of ToDoList, maybe you don't want to allow multiple identical items, or you don't want them inserted out of alphabetical order. And if you want to do something whenever the list changes, you'd have to override every method that can modify the seq.
I've been doing object-oriented programming since 1984 (I started in Smalltalk-80.) I have only very rarely found a need to subclass a general-purpose class like Array, Set, Dictionary, etc. And those times have usually been for advanced low-level stuff (like database models), or for complex optimizations (like highly scalable mutable arrays.)
So nim's way is to make you implement all the methods you need by hand ?
No, Nim's way is to not do wrongheaded subclassing, as explained.