Hello, I am a CS student and I'm currently preparing a talk about Nim for a Uni exam. In my presentation I try to show some of Nim's most advanced, less main-stream features and in my opinion two of these are multi-methods and concepts. I have a few questions regarding them.
1. How is multi-method dispatch implemented? I read the manual section about multi-methods and it says that multi-method dispatch is done through dispatch trees. I looked at the source code generated for some examples and saw that Nim does multi-method dispatch by examining the parameters' type through a case-analysis. As I understand it, there's no code generated for dispatch trees nor any tree structure at runtime. So how do dispatch trees come into play?
2. What is the use case behind concepts? They seem to be somewhat close to interfaces/traits, but you can't explicitly implement a concept. You can only conform to it indirectly by behaving like it.
Thank you in advance for any answer
I don't think Multimethods and Concepts are that cool. Probably because I don't like OOP. Also many languages have some thing like that.
I would pick some thing else more existing like how you can grow from procs -> templates -> macros as you need more and more power. Very few languages have this.
Also the async await implemented as macros is also pretty cool. Very few languages this is possible in.
Also its pretty cool how nim compiles to and interoperates with not only c and c++ but also objectiveC and javascript. Very few languages do this.
Also pretty cool how you can always drop lower and lower in levels: Start at high level objects oriented GC'd things, transition to tightly packed C structures with manual memory management, and then transition to even lower inline assembly. Very few languages do this. With nim and enough time you can always be equal with C or beat C because you can always make nim produce equivalent C code.
I don't like OOP either, but the exam is about programming constructs that are "unusual" in the sense that they are not main-stream and are recently emerging.
Multi-methods are present in other languages too but I wouldn't say that they have entered the "average" programmer's life (for the better probably). The same kinda goes for concepts even tough type classes/traits/... are seeing an increased use.
This was just to clarify why I chose those things. I also talk about metaprogramming through macros/templates and other cool things (of which Nim is full).
Thank you for your suggestions, async and await as macros seem like something I could add!
I wouldn't pick methods because they might be removed, and concepts are a bit too young (but C++17 will add concepts as well so ...).
For me the main draw would be macro/AST manipulation, Nim is not alone (C++ has template, Rust has macros that somewhat works on AST, Julia has macros, Python and D has string mixin and Lisp ... well Lisp has everything) but Nim definitely has the best macro system.
Now regarding your questions:
I wouldn't pick methods because they might be removed
Does anyone know how likely this is to happen? I'm always unsure whether to avoid method when designing a polymorphic interface.
Does anyone know how likely this is to happen? I'm always unsure whether to avoid method when designing a polymorphic interface.
Methods are here to stay.
One super cool use of macros that you can showcase is syntactic sugar for functional programming using: -> and => from future (in stable) or sugar (in devel).
The library that best showcase this is nimfp, for example takeWhile:
https://github.com/vegansk/nimfp/blob/master/src/fp/stream.nim#L126-L128
proc takeWhile*[T](xs: Stream[T], p: T -> bool): Stream[T] =
## Takes elements while `p` is true
xs.foldRight(() => T.empty(), (x: T, y: () -> Stream[T]) => (() => (if x.p: cons(() => x, y) else: T.empty)))
-> is for higher-order function signature: T -> bool is sugar for proc(foo: T): bool => is for lambda function inline implementation: () => T.empty() is a function that takes no parameter and returns T.empty so equivalent to proc(): T = T.empty()
Others examples include