Anonymous user
Compound data type: Difference between revisions
rearranges in order of the language.
m (added whitespace before the TOC (table of contents), added a ;Task: and ;Related task: (bold) headers.) |
(rearranges in order of the language.) |
||
Line 233:
int y;
} Point;</lang>
=={{header|C sharp|C#}}==▼
<lang csharp>struct Point▼
{▼
public int x, y;▼
public Point(int x, int y) {▼
this.x = x;▼
this.y = y;▼
}▼
}</lang>▼
=={{header|C++}}==
Line 261 ⟶ 272:
Point<float> point2 = { 1.7, 3.6 };</lang>
Of course, a constructor can be added in this case as well.
▲=={{header|C sharp|C#}}==
▲<lang csharp>struct Point
▲{
▲ public int x, y;
▲ public Point(int x, int y) {
▲ this.x = x;
▲ this.y = y;
▲ }
▲}</lang>
=={{header|Clean}}==
Line 482:
x = A, y = 53.420
</pre>
=={{header|F_Sharp|F#}}==▼
See the OCaml section as well. Here we create a list of points and print them out.▼
<lang fsharp>type Point = { x : int; y : int }▼
let points = [▼
{x = 1; y = 1};▼
{x = 5; y = 5} ]▼
Seq.iter (fun p -> printfn "%d,%d" p.x p.y) points</lang>▼
=={{header|Factor}}==
Line 560 ⟶ 570:
! rational array element & store
end program typedemo ! as integer array</lang>
▲=={{header|F_Sharp|F#}}==
▲See the OCaml section as well. Here we create a list of points and print them out.
▲<lang fsharp>type Point = { x : int; y : int }
▲let points = [
▲ {x = 1; y = 1};
▲ {x = 5; y = 5} ]
▲Seq.iter (fun p -> printfn "%d,%d" p.x p.y) points</lang>
=={{header|Go}}==
Line 794:
}
point = new Point(1, 2);</lang>
=={{header|JSON}}==▼
<lang json>{"x":1,"y":2}</lang>▼
=={{header|jq}}==
Line 806 ⟶ 802:
Using this approach, one can distinguish between objects of type "Point" and those that happen to have keys named "x" and "y".
▲=={{header|JSON}}==
▲<lang json>{"x":1,"y":2}</lang>
=={{header|Julia}}==
Line 867:
Var:Point point;
}</lang>
=={{header|Lasso}}==▼
In Lasso, a point could just be stored in the pair type. However, assuming we want to be able to access the points using the member methods [Point->x] and [Point->y], let's just create a type that inherits from the pair type:▼
<lang lasso>define Point => type {▼
parent pair▼
public onCreate(x,y) => {▼
..onCreate(#x=#y)▼
}▼
public x => .first▼
public y => .second▼
}▼
local(point) = Point(33, 42)▼
#point->x▼
#point->y</lang>▼
{{out}}▼
<pre>33▼
43</pre>▼
=={{header|LFE}}==
Line 915 ⟶ 936:
true
</pre>
▲=={{header|Lasso}}==
▲In Lasso, a point could just be stored in the pair type. However, assuming we want to be able to access the points using the member methods [Point->x] and [Point->y], let's just create a type that inherits from the pair type:
▲<lang lasso>define Point => type {
▲ parent pair
▲ public onCreate(x,y) => {
▲ ..onCreate(#x=#y)
▲ }
▲ public x => .first
▲ public y => .second
▲}
▲local(point) = Point(33, 42)
▲#point->x
▲#point->y</lang>
▲{{out}}
▲<pre>33
▲43</pre>
=={{header|Logo}}==
Line 1,588 ⟶ 1,587:
=={{header|Rust}}==
===Structs===
There are three kinds of <code>struct</code>s in Rust, two of which would be suitable to represent a point.
====C-like struct====
<lang rust> // Defines a generic struct where x and y can be of any type T
struct Point<T> {
Line 1,602 ⟶ 1,601:
} </lang>
====Tuple struct====
These are basically just named tuples.
<lang rust>struct Point<T>(T, T);
Line 1,609 ⟶ 1,608:
println!("{},{}", p.0, p.1);
}</lang>
===Tuples===
<lang rust> fn main() {
let p = (0.0, 2.4);
|