> 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
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
> 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. ;-)
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
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
@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
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
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]
> 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
> 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
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
The allure of undocumented behaviors ;)
@dataPulverizer, if you already use pointers you can use generic procs, why do
you need inheritance for dispatch?
> you can't mix inheritance with un-managed raw pointers.
I can do it. In fact, I did in Nim's allocator.
> you can't mix inheritance with un-managed raw pointers.
I can do it. In fact, I did in Nim's allocator.
Isn't the point of inheritance method dispatch? Why isn't that a separate issue
from objects that are dissociated from the garbage collector?
> 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?
`ptr object of RootObj` seems very strange, you can't mix inheritance with
un-managed raw pointers.
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!
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 =
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().
20 matches
Mail list logo