Re: Workflow: how do I make a cpp lib in nim?
@jyapayne Thank you for detailing all that! I had been using 0.18.0 because I'm on Win and that was the version available to download in zipped binary format. But apparently this requires new fixes in latest devel, as 0.18.1 worked nicely.
Re: Workflow: how do I make a cpp lib in nim?
Oops, I didn't see that you changed the source. It should still work though, but let me check.
Re: Workflow: how do I make a cpp lib in nim?
@gibson, here's what seemed to work for me. I changed `mylib.nim` to be like this, but I only did so to get rid of the duplicate name: import strutils, sequtils, os, parsecsv, streams, future proc parseCSVListLine*(str: cstring): cstringArray {.exportc.} = var ss = newStringStream($str) var p: CsvParser p.open(ss, "parseCSVListLine()") discard p.readRow() return allocCstringArray(lc[ col.strip() | (col <- items(p.row)), string]) Run I changed the `main.cpp` as follows: // main.cpp #include #include "mylib.h" using namespace std; // for demo int main() { NimMain(); // Need to add this for Nim's GC calls and other Nim related things parseCSVListLine("one,two,three"); return 0; } Run And here's how I made it work using the latest Nim devel branch: nim cpp --noMain --noLinking --header:mylib.h mylib.nim Run And to compile the c++ code: g++ -I/home/joey/Nim/lib -I/home/joey/.cache/nim/mylib_d/ -o main main.cpp ~/.cache/nim/mylib_d/*.cpp -w Run note that in the above command, the `-I` calls are a bit finicky. You need to specify the full path. Also I compiled on Ubuntu, so windows mileage may vary. Let me know if this works for you.
Re: Workflow: how do I make a cpp lib in nim?
@jyapayne I think "can easily extend the example to c++" is giving me too much credit as I don't often deal with FFI scenarios. Does anyone have an example where this has been done? Either wrapping a nim->c library in c++, or wrapping a nim->c++ library in c++?
Re: Confused with Nim OOP tutorials. Please clarify straight forward examples
> For a library, when you need to build a heterogeneous collection of objects > with types that can be user defined. Why don't object variants work here, instead of OOP? I don't see the need to introduce method for this case. The only thing OO provides is open recursion, which I haven't needed in most of the code I write.
Re: How to call a proc of Base class from a derived class ?
Here is your example translated to Nim using a method for "cry". type Animal* = ref object of RootObj weightOfAnimal*: int method cry(x: Animal) {.base.} = echo "Animal is crying..." type Dog* = ref object of Animal name*: string proc newDog*(sName: string = ""): Dog = new(result) result.name = sName method cry*(x: Dog) = procCall x.Animal.cry() echo "But Dog is barking..." let myAnimal1 = newDog() let myAnimal2 = Dog(name: "Médor") myAnimal1.cry myAnimal2.cry Run Some explanations. I have used ref objects but could have used object instead. The only reason here is for convenience as I can use a proc "newDog" to create Dog objects. There is no use for a proc "newAnimal" as the standard way to create Animal objects is sufficient (weight is initialized at 0 anyway). There is no real need for a proc "newDog" here but I have kept it for demonstration purpose. To call the Animal "cry", you need to use procCall to bypass the dynamic binding. And you need a conversion to Animal of course. This way, you have full control over what you want to call in the class hierarchy. To create a Dog object, we can use the "newDog" proc or we can simply provide the values of the fields to initialize. For the name "Médor" for the dog, this is supposed to be a common dog name in French (never encountered a dog with this name though :-)). I don’t know the common dog name in English. Hope this helps.
Re: How to call a proc of Base class from a derived class ?
Sorry it should have been methods instead of proc. But seems like conversion doesn't work.
Re: How to call a proc of Base class from a derived class ?
@mratsim, Error: attempting to call undeclared routine: 'base'
Re: How to call a proc of Base class from a derived class ?
You need to use procCall. It was recently document in the dev doc: [https://github.com/nim-lang/Nim/commit/4ae9198493d85382c80a5c9436b86c5a6f3483c1](https://github.com/nim-lang/Nim/commit/4ae9198493d85382c80a5c9436b86c5a6f3483c1)
Re: How to call a proc of Base class from a derived class ?
You can convert before calling: type Animal = ref object of RootObj Dog = ref object of Animal proc cry(x: Animal) {.base.} = echo "Foobar" proc cry(x: Dog) = echo "Bark" let x = Dog() x.cry x.Animal.cry Run
Re: [Status Grant Proposal] Make Nim suitable for interactive programming
I'm bumping this to keep it visible for couple of more weeks
Re: please advise: threading/GC - how make it work?
sleep(0) won, with other mods. thanks for visiting github, made a lot of progress lately - still no docs, but you are welcomed :)
Re: release only runtime crash
This may happen if nim GC loses track of your json objects, "deallocates" them, and later reuses their memory for something else. As a test, patch `json.nim` by adding a finalizer to all allocated JsonNodes, and see if it is getting called when it's not supposed to. Revealing your source code for our review could also help :)
Re: How to call a proc of Base class from a derived class ?
jlp765, I think that link and your post is not that helpful for his question. I think question is more related to "super" methods as it is called for example in Ruby. For Nim we have the procCall defined in module system, I think it may be applied for this case, but I have never tried, as I have not yet done OOP in Nim. Maybe the blog posts of Mr Goran Krampe are helpful, but they are from 2014, I have never read them again since then. I guess procCall may be used to call the base class method, but what when we have an Object of type C, which is derived from type B, which is again derived from base Type A. So we have object of type C and want to call method defined for type B?
Re: Cannot call function with argument of Nim from Python3
> But I got error message That one is fixed. Now regarding your first question. When calling nim function from a foreign runtime you have to make sure that nim's GC is configured properly. Quick way to check it is to do `GC_disable()` as the very first operation of your externally called function. If it works, it means that nim GC is initialized with wrong stack bottom (see `nimGC_setStackBottom`). `nimpy` does that for you, but iirc you'll need a more recent nim version.