0

I was trying to understand references in Nim comparing ref to pointers in C (I know that I should not compare these two languages but I understand way better in this way). Then something came up to my mind... Are ref objects always allocated in the heap? They ain't gonna create an object and then point to it, right?

type
  Person = object
    value: int
  PersonRef = ref Person

var obj1 = PersonRef(value: 1)
var obj2: PersonRef
new(obj2)

This Nim code should be similar to this C code?

#include <stdlib.h>

struct Person {
  int value;
}
typedef struct Person* PersonRef;

int main() {
  PersonRef obj1 = malloc(sizeof(struct Person))
}

So to recap, my questions are:

  • Are ref objects always allocated in the heap?
  • What's the real difference between new and common object constructors (in ref objects ofc)
  • At this point, is the var parameter modifier just a reference to the object that we mark with exactly var

Sorry for my perhaps not coincident question but I was in a rush

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
std124_lf
  • 134
  • 2
  • 9
  • I have very (very) little experience with Nim, so I won't post an answer in order not to say stupid/incorrect things, *but anyway* [this SO question](https://stackoverflow.com/questions/22096880/what-is-the-model-of-value-vs-reference-in-nim) and [this Nim forum discussion](https://forum.nim-lang.org/t/1207) may help you better understand the topic. It appears that `ref` objects are always allocated on the heap, and they are traced by the Nim's garbage collector. – Luca Polito Sep 27 '21 at 15:32

1 Answers1

2

I was trying to understand references in Nim

What have you read already? That topic is explained in many tutorials and in some videos. My explanations is located at

http://ssalewski.de/nimprogramming.html#_value_objects_and_references

That are a few pages that you may intent to read, with the last section

http://ssalewski.de/nimprogramming.html#_references_to_objects

In case you should really read that carefully, and something is still unclear, then please create an issue at https://github.com/StefanSalewski/NimProgrammingBook If that is not possible for you, then you may send me a private email too.

[EDIT]

OK, I just found the video about object references, it is very fresh still. It is the last in this list:

https://www.youtube.com/playlist?list=PLvwc2YT9MFOlPPexrsY-t7BNTdg2Vsx06

I am not sure how well it explains your questions, have not really watched it myself. I found that video series starting at https://nim-lang.org/documentation.html where many more tutorials are listed.

For your concrete questions: Nim references are managed pointers, and indeed values referenced by Nim references are allocated on the heap, with new() or the "Constructor" MyType(myField: myValue) or a dedicated constructor function like newButton(). Note that Nim does not have real constructors like C++. Function names like newButton() are just a convention, that function may initialize the object. Functions to initialize value objects located on the stack are generally called initMyType(), but again that is only a convention. Calling new() or a function with the type name like MyType(myField: myValue) is similar, but new always allocates an instance on the heap with default (binary zero) content, while a MyType() call can be used for value or reference objects, and most importantly can initialize fields. So new() is some kind of redundant, I have seen discussions about removing new() for Nim 2.0. And for your other questions, like the var keyword for procedure parameters -- well all that is explained in the various tutorials, in more detail, better wording and less errors as in this text.

Sorry for my perhaps not coincident ANSWER but I was in a rush :-)

Salewski
  • 164
  • 1
  • 2
  • 1
    Thanks now I get things that before I couldn't get. I read your blog and the explanations were simple yet concise thus I must say keep up the good work. – std124_lf Sep 28 '21 at 18:54