I'm still new to Nim (got it a couple of days ago).
Both versions work. Which one is better? (It is not clear, or I have missed something in docs.)
The first:
var sdl_rect : Rect
sdl_rect = ( (cint)0, (cint)0, (cint)10, (cint)10 )
# usage: (addr sdl_rect), when ptr Rect is required
The second:
var sdl_rect : ref Rect
new(sdl_rect)
sdl_rect.x = 0
sdl_rect.y = 0
sdl_rect.w = 10
sdl_rect.h = 10
# usage: sdl_rect, when ptr Rect is required
That is not an easy topic for a beginner, you may start with
https://peterme.net/nim-types-originally-a-reddit-reply.html
I disagree, at least regarding the question in the title.
A ref is always preferable unless one needs a pointer. And the heap vs. stack is not Nim specific.
Use var sdl_rect : Rect If you ate going to process a lot of them sequentially.
Use var sdl_rect : ref Rect If you only handle those instances individually or if you use OOP or if there is a lot of fields (data) in the object
@aguspiza, I think you are confused about mutability/immutability, var and ref.
var means mutable. ref means object on the heap with reference semantics. They are somewhat orthogonal concepts.