I just read about the division of object-oriented languages in class-based and prototype-based. Through Python and Java I'm more used to the class-based approach, but with JavaScript I know a bit about prototype-based OOP as well.
Now I am wondering in what category falls Nim? I guess it is rather class-based, but it doesn't look very strongly like it to me. I'm sure the language theorists know more...
Nim is not OO lang :) I think you can achieve either class or prototype-based style in Nim. It's up to you.
This is wrong. Nothing at https://en.wikipedia.org/wiki/Prototype-based_programming applies to Nim.
Achieve != Being I may be wrong about what is a prototyping style but for me it lays down to this:
#prototype
type Animal = object
age: int
# object with a prototype
type Alpaca = object
self: Animal
I'm not using wiki, just my sense.
The former is greatly preferable.
If I'm wrong so be it
An interesting attitude. I prefer to converse with people who care about getting things right.
interesting attitude. I prefer to converse with people who care about getting things right.
I have nothing wrong with that :) Thanks for clarifying and your time.You can tell a prototype-based language because it lets you implement and override methods on _individual objects, not just on classes. (Prototype languages may not even have classes at all; JS didn’t until recently.)
There aren’t many such languages. Self was the first, then there’s JS. Several languages for writing text adventures use prototypes, like Inform, because it’s a very useful feature for such games (e.g. you can override the verb “drop” for the bottle-of-nitroglycerin object, without having to give it a special one-off subclass.)
Overriding methods per object in Nim can happen, sometimes I've seen this sort of handmade vtablish thing:
import sugar, strformat
type
Nimterface = object
m: proc(x:int):string{.nimcall.}
proc call(o: Nimterface,x:int):string = o.m(x)
let obj1 = Nimterface(m:(x:int)=> &"one:{x}")
let obj2 = Nimterface(m:(x:int)=> &"two:{2*x}")
echo obj1.call(5)
echo obj2.call(7)
That's a bit prototype ish Aha. Yes. You'd need to overload the dot operator for that, and do your own lookup of fields in a table somewhere with bookkeeping of types and raw pointers to the value? Bit more involved.
None of what I'm saying is to imply that nim "is a" oo or proto language, more wondering how easily/cleanly one could emulate/replace those behaviours.