Pointers and references: Difference between revisions

m
→‎{{header|Phix}}: added note about classes
(→‎{{header|Wren}}: Clarified the distinction between value and reference types.)
m (→‎{{header|Phix}}: added note about classes)
Line 1,252:
Of course in a reference counted language like Phix, playing directly with the innards like that may have dire unexpected side effects.
UPDATE: as of 0.8.1, Phix has classes which ''are'' reference types, as are dictionaries.<br>
Phix does not have references, however everything is passed by reference, with copy-on-write semantics. Unless the source and destination are the same, and it is local, so it cannot possibly be referenced elsewhere, in which case automatic pass-by-reference is used, which for Phix means skipping the reference counting. For example:
<lang Phix>sequence s
Line 1,258 ⟶ 1,259:
Several of the builtins, for instance s = append(s,thing) have a similar optimisation, as long as s occurs on both the rhs and lhs, and less any need for it to be local (since the builtins are non-recursive leaf routines).
 
UPDATE: as of 0.8.1, class instance and dictionary parameters ''can'' be updated without needing to be reassigned on return.<br>
Phix takes the view that there should be as few as possible unintended side effects. If you see a line of code such as s = myfunc(...) then you have the full list of all [local] variables it will modify on the lhs, without needing to pick through the parameter list, though of course it might trample on a few external global or static variables. Personally I find that approach often helps me narrow down all the places where a bug might lurk much quicker, and that is certainly worth an occasional bit of extra typing on the lhs.
 
7,803

edits