Re: How to instantiate `ptr object`

2020-05-19 Thread snej
> You can use closures or pass the data parameter as an input to the function. In some sense closures and objects are isomorphic, as are functional and OOP. There’s a famous old _koan_ about this: > The venerable master Qc Na was walking with his student, Anton. Hoping to > prompt the master

Re: How to instantiate `ptr object`

2020-05-19 Thread Stefan_Salewski
y: 1.2E3 Run Thanks for that hint. Of course the book is in an very early state still, I wrote most of the two first chapters in April. But I hope and think that it can be already useful for beginners with nearly no CS knowledge. I have just yesterday applied a lot fixes

Re: How to instantiate `ptr object`

2020-05-18 Thread Araq
> For your example ARC makes a gigantic difference. Yeah, known problem. Method dispatch got super slow for ARC. Not sure yet how to fix it. I can do it the old way without problems, but where is the fun in that. ;-)

Re: How to instantiate `ptr object`

2020-05-18 Thread DIzer
2 Stefan_Salewski - is your book published or it is "in process" state? I saw some referred (in the text) mistypings like: var mean = 3.0 / 7.9 x: float = 12 y: 1.2E3 Run

Re: How to instantiate `ptr object`

2020-05-16 Thread mratsim
For machine learning in particular deep learning this is how I do it: 1\. I use inheritance, but only to be able to store the objects in the same container, **never for dispatch**. Example on neural network layer, called `Gate`, re-using the terminology from Andrej Karpathy's [Hacker's Guide to

Re: How to instantiate `ptr object`

2020-05-15 Thread dataPulverizer
@mratsim Thank you for your detailed and informative response, also I didn't know about the do statement in Nim that's something else new for me. My main takeaway is that I don't always have to use runtime resolution, especially because the kind of code I write is mostly statistics/machine

Re: How to instantiate `ptr object`

2020-05-14 Thread Stefan_Salewski
For your example ARC makes a gigantic difference. (I put your global code in a main proc) import times type FooBase = ref object {.inheritable.} dummy: int type Foo{.final.} = ref object of FooBase value : float32 proc inplace_add_proc(x: var

Re: How to instantiate `ptr object`

2020-05-14 Thread mratsim
You can use closures or pass the data parameter as an input to the function. Here is an example with closures type AbstractKernel[T] = proc(dst: var T, src: Matrix[T], i, j: int) {.closure.} Matrix[T] = object dim: array[2, int] data: seq[T]

Re: How to instantiate `ptr object`

2020-05-14 Thread dataPulverizer
> No you don't, you can have AbstractKernel be a procNo you don't, you can have > AbstractKernel be a proc. > > > type AbstractKernel[F] = proc(loc: var F) {.nimcall.} > > > Run I don't understand and have a couple of questions. Firstly, what would be the syntax for

Re: How to instantiate `ptr object`

2020-05-14 Thread mratsim
> At compile time yes you could just use procs but you need polymorphism for > runtime dispatch. No you don't, you can have AbstractKernel be a proc type AbstractKernel[F] = proc(loc: var F) {.nimcall.} Run

Re: How to instantiate `ptr object`

2020-05-13 Thread dataPulverizer
One of the things that intrigued me about Nim was it's object system, you could create value, reference and pointer types with/without inheritance. As I understand it, an important distinction between ptr and ref classes is that ref classes are managed by the garbage collector while ptr classes

Re: How to instantiate `ptr object`

2020-05-13 Thread mratsim
The allure of undocumented behaviors ;) @dataPulverizer, if you already use pointers you can use generic procs, why do you need inheritance for dispatch?

Re: How to instantiate `ptr object`

2020-05-13 Thread Araq
> you can't mix inheritance with un-managed raw pointers. I can do it. In fact, I did in Nim's allocator.

Re: How to instantiate `ptr object`

2020-05-13 Thread Araq
> you can't mix inheritance with un-managed raw pointers. I can do it. In fact, I did in Nim's allocator.

Re: How to instantiate `ptr object`

2020-05-13 Thread dataPulverizer
Isn't the point of inheritance method dispatch? Why isn't that a separate issue from objects that are dissociated from the garbage collector?

Re: How to instantiate `ptr object`

2020-05-13 Thread Stefan_Salewski
> you can't mix inheritance with un-managed raw pointers That is an interesting point, I was wondering about it this morning already when I wrote my first reply. I am still not sure. I could generally imagine that inheritance can work with with un-managed raw pointers. But maybe not in Nim?

Re: How to instantiate `ptr object`

2020-05-13 Thread mratsim
`ptr object of RootObj` seems very strange, you can't mix inheritance with un-managed raw pointers.

Re: How to instantiate `ptr object`

2020-05-13 Thread snej
I wasn’t aware of that book, though I’ve been guzzling from the Nim-documentation firehose for a week now. You should get it linked from the Nim-lang “learning” page!

Re: How to instantiate `ptr object`

2020-05-13 Thread dataPulverizer
I just wasn't sure whether when allocating for objects you have to account for any "gubbings", just cautious I guess: type MyTypeA = ptr object of RootObj x: int64 MyTypeB = ptr object of RootObj x: array[0..4, int64] var x0 =

Re: How to instantiate `ptr object`

2020-05-12 Thread Stefan_Salewski
That is also covered in my book in detail. Have you starting reading? [http://ssalewski.de/nimprogramming.html#_allocating_objects](http://ssalewski.de/nimprogramming.html#_allocating_objects) alloc(), create(), dealloc().