Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Would you like to elaborate on the meaning of value based versus reference based?


What does this print?

   a = [1, 2]
   b = a
   a[0] = 3
   print(b[0])
In JavaScript, Java, C#, Dart, Python, etc. it will print 3 because a and b are references to values.

In C++, Rust and TCL, (and maybe Go? I don't remember) it will print 1 because a and b are values.

In value based languages you need explicit syntax to make references and dereference.

In reference-based languages you need explicit syntax to copy values. Often this is omitted entirely which is quite annoying. Until very recently the standard solution to copy a variable was to serialise it to JSON and back again!!

Also worth noting that for immutable values (i.e. all values in Haskell, strings in JavaScript, etc.) there's no observable difference between the two.


Thanks for taking the time to explain that. I don't think I ever knew this a clearly as I now do!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: