DanielSokil (2) [Avatar] Offline
#1
Hello, What is a good example of when to use reference types vs non-reference types?

Here is a non-ref object:
type
    Person = object
        name: string
        age: int

var person: Person
person.name = "Daniel"


Here is a ref object:
type
    PersonObj = object
        name: string
        age: int
    PersonRef = ref PersonObj # reference type

proc setName(name: string, person: PersonRef) =
    person.name = name

var person1 = PersonRef()
setName("Daniel", person1)


Why should I use one over the other? They both seem to be achieving the same thing.
Since ref object is live on the heap, and non-ref is on the stack, Is that the only difference?
dom96 (75) [Avatar] Offline
#2
This should help you out: https://forum.nim-lang.org/t/1207
DanielSokil (2) [Avatar] Offline
#3
Thanks, Dominik!