Pointers and references: Difference between revisions

Add Ecstasy example
imported>Acediast
(→‎{{header|COBOL}}: Syntax highlighting.)
(Add Ecstasy example)
 
(One intermediate revision by one other user not shown)
Line 675:
→ 666
</syntaxhighlight>
 
=={{header|Ecstasy}}==
In Ecstasy, all values are references to objects. These references are like pointers in C, but address information is not accessible, and no arithmetic operations can be performed on them. Additionally, each reference is itself an object, providing reflective information about the reference.
 
Objects are always accessed and manipulated through references, using the <code>.</code> ("dot") operator.
 
Ecstasy uses call-by-value. When passing arguments, all arguments are references, passed by value.
 
<syntaxhighlight lang="ecstasy">
module test {
@Inject Console console;
 
public class Point(Int x, Int y) {
@Override String toString() = $"({x},{y})";
}
 
void run() {
Point p = new Point(0, 0);
p.x = 7;
p.y = p.x;
console.print($"{p=}");
 
// obtain the reference object itself
Ref<Point> r = &p;
console.print($"{r.actualType=}");
}
}
</syntaxhighlight>
 
{{out}}
<pre>
x$ xec test
p=(7,7)
r.actualType=Point
</pre>
 
=={{header|Forth}}==
Line 2,168 ⟶ 2,203:
 
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.
<syntaxhighlight lang="ecmascriptwren">// This function takes a string (behaves like a value type) and a list (reference type).
// The value and the reference are copied to their respective parameters.
var f = Fn.new { |s, l|
162

edits