I recently had a look at the "Nim for Python programmers" wiki page and I'd like to suggest some changes. I asked in the #nim IRC channel and @mratsim suggested I should write down the changes first to discuss them on the Nim forum.
So here are the potential changes: https://gist.github.com/sschwarzer/ff9f3f7092f132b17e2b33e7a5a2fe81 . What do you think?
I would like to see this: "Object variants as lightweight alternative to inheritance" explained. :)
Object variants are not really a lightweight alternative to inheritance.
Both concepts are explained in various tutorials, I tried
http://ssalewski.de/nimprogramming.html#_object_orientated_programming_and_inheritance
Well technically you can replace all uses of inheritance with object variants (I think). But in some cases it will get too verbose :)
In the sense that all turing-complete languages are equivalent...
With variants, you have to know and handle all the possible classes at every "method" implementation.
With inheritance, the implementations can be done independently.
Equivalent capability, of course, but vastly different from a management/maintenance perspective.
"With variants, you have to know and handle all the possible classes at every "method" implementation." except you don't, you can just do
case myobj.kind
of MyKind:
# do stuff...
else:
discard
Except you don't, you can just do
if I want to add a variant to a variant object I need to edit the object itself, while I can also inherit from e.g. a type defined in a library.
You can use macros to provide class-like capabilities to object variants (i.e. constructing the object variant from multiple modules)
The only limitation is that in other languages like C++, you can add a new subtype even when the library is compiled as a DLL while with macros you need compilation from source. But current Nim methods have the same limitation.