The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

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!