Jump to content

Pointers and references: Difference between revisions

Added Wren
m (→‎{{header|Raku}}: Fix comments: Perl 6 --> Raku)
(Added Wren)
Line 1,653:
Dim anothervariable as Object
Set anothervariable = sameplevariable</lang>
 
=={{header|Wren}}==
Wren has references to objects (which are tracked by the garbage collector) but doesn't support pointers (which can point to arbitrary addresses in memory) as such.
 
References are always implicit - you have to remember which types are reference types and which are value types.
 
Of the built-in classes only Num, String, Bool, Range, Class and Null are value types. The remainder and all user-defined classes are reference types.
 
The following example illustrates the difference by passing both value and reference type parameters to a function. Note that in Wren parameters are always passed by value.
<lang ecmascript>// This function takes a string (value type) and a list (reference type).
// The value and the reference are copied to their respective parameters.
var f = Fn.new { |s, l|
if (s.type != String) Fiber.abort("First parameter must be a string.")
if (l.type != List) Fiber.abort("Second parameter must be a list.")
// add the first argument to the list
l.add(s) // the original 'l' will reflect this
// mutate the first argument by reversing it
s = s[-1..0] // the original 's' will not reflect this
}
 
var s = "wren"
var l = ["magpie", "finch"]
f.call(s, l)
System.print(l)
System.print(s)</lang>
 
{{out}}
<pre>
[magpie, finch, wren]
wren
</pre>
 
=={{header|XPL0}}==
9,485

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.