Compound data type: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(12 intermediate revisions by 11 users not shown)
Line 18:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">T Point
Int x, y
 
F (x, y)
.x = x
.y = y</langsyntaxhighlight>
 
=={{header|ACL2}}==
<langsyntaxhighlight Lisplang="lisp">(defstructure point
(x (:assert (rationalp x)))
(y (:assert (rationalp y))))
Line 34:
(assign p1 (update-point (@ p1) :x 3)) ; Update the x value
(point-x (@ p1))
(point-p (@ p1)) ; Recognizer for points</langsyntaxhighlight>
 
{{out}}
Line 47:
 
NESASM3 syntax:
<langsyntaxhighlight lang="6502asm">MAX_POINT_OBJECTS = 64 ; define a constant
 
.rsset $0400 ; reserve memory storage starting at address $0400
point_x .rs MAX_POINT_OBJECTS ; reserve 64 bytes for x-coordinates
point_y .rs MAX_POINT_OBJECTS ; reserve 64 bytes for y-coordinates</langsyntaxhighlight>
 
VASM syntax:
<langsyntaxhighlight lang="6502asm">MAX_POINT_OBJECTS equ 64
 
point_ram equ $0400
point_x equ point_ram
point_y equ point_ram+MAX_POINT_OBJECTS</langsyntaxhighlight>
 
So, for example, let's say we want to load our third (zero-indexed) point variable and copy it to zero page RAM addresses $00 and $01. We would do the following:
<langsyntaxhighlight lang="6502asm">MAX_POINT_OBJECTS equ 64
 
point_ram equ $0400
Line 71:
STA $00
LDA point_y,x
STA $01</langsyntaxhighlight>
 
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
DEFINE REALPTR="CARD"
Line 101:
PrintR(p2.rx) Print(",")
PrintR(p2.ry) Print(")")
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Compound_data_type.png Screenshot from Atari 8-bit computer]
Line 110:
 
=={{header|ActionScript}}==
<langsyntaxhighlight lang="actionscript">package
{
public class Point
Line 123:
}
}
}</langsyntaxhighlight>
 
=={{header|Ada}}==
===Tagged Type===
Ada tagged types are extensible through inheritance. The reserved word ''tagged'' causes the compiler to create a tag for the type. The tag identifies the position of the type in an inheritance hierarchy.
<langsyntaxhighlight lang="ada">type Point is tagged record
X : Integer := 0;
Y : Integer := 0;
end record;</langsyntaxhighlight>
 
===Record Type===
Ada record types are not extensible through inheritance. Without the reserved word ''tagged'' the record does not belong to an inheritance hierarchy.
<langsyntaxhighlight lang="ada">type Point is record
X : Integer := 0;
Y : Integer := 0;
end record;</langsyntaxhighlight>
 
====Parameterized Types====
An Ada record type can contain a discriminant. The discriminant is used to choose between internal structural representations. Parameterized types were introduced to Ada before tagged types. Inheritance is generally a cleaner solution to multiple representations than is a parameterized type.
<langsyntaxhighlight lang="ada">type Person (Gender : Gender_Type) is record
Name : Name_String;
Age : Natural;
Line 152:
null;
end case;
end record;</langsyntaxhighlight>
In this case every person will have the attributes of gender, name, age, and weight. A person with a male gender will also have a beard length.
 
Line 158:
===Tagged Type===
ALGOL 68 has only tagged-union/discriminants. And the tagging was strictly done by the ''type'' (MODE) of the members.
<langsyntaxhighlight lang="algol68">MODE UNIONX = UNION(
STRUCT(REAL r, INT i),
INT,
Line 165:
STRUCT(REAL rr),
STRUCT([]REAL r)
);</langsyntaxhighlight>
To extract the apropriate member of a UNION a '''conformity-clause''' has to be used.
<langsyntaxhighlight lang="algol68">UNIONX data := 6.6;
CASE data IN
(INT i): printf(($"r: "gl$,i)),
Line 175:
OUT
printf($"Other cases"l$)
ESAC;</langsyntaxhighlight>
The '''conformity-clause''' does mean that ALGOL 68 avoids the need for
[[duck typing]], but it also makes the tagged-union kinda tough to use,
Line 182:
ALGOL 68 record types are not extensible through inheritance but they
may be part of a larger STRUCT composition.
<langsyntaxhighlight lang="algol68">MODE POINT = STRUCT(
INT x,
INT y
);</langsyntaxhighlight>
====Parameterized Types====
An ALGOL 68 record type can contain a tagged-union/discriminant. The
tagged-union/discriminant is used to choose between internal structural
representations.
<langsyntaxhighlight lang="algol68">MODE PERSON = STRUCT(
STRING name,
REAL age,
Line 198:
VOID
) gender details
);</langsyntaxhighlight>
In this case every PERSON will have the attributes of gender details, name, age,
and weight. A PERSON may or may not have a beard. The sex is implied by the tagging.
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin
% create the compound data type %
record Point( real x, y );
Line 212:
% access the fields of p - note Algol W uses x(p) where many languages would use p.x %
write( x(p), y(p) )
end.</langsyntaxhighlight>
 
=={{header|AmigaE}}==
<langsyntaxhighlight lang="amigae">OBJECT point
x, y
ENDOBJECT
Line 228:
pt.y := !3.14
END pt
ENDPROC</langsyntaxhighlight>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
 
/* ARM assembly Raspberry PI */
Line 375:
iMagicNumber: .int 0xCCCCCCCD
 
</syntaxhighlight>
</lang>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="arturo">point: #[
x: 10
y: 20
]
 
print point</langsyntaxhighlight>
 
{{out}}
Line 394:
There are numerous ways to do this. The simplest is to use an "unboxed" tuple type:
 
<langsyntaxhighlight ATSlang="ats">typedef point (t : t@ype+) = @(t, t)
val p : point double = (1.0, 3.0)</langsyntaxhighlight>
 
If one insists both that the type be unique (as opposed to an alias for a tuple) and that the notation to create a point be '''Point (x, y)''', then the following works:
<langsyntaxhighlight ATSlang="ats">datatype point (t : t@ype+) =
| Point of (t, t)
val p : point double = Point (1.0, 3.0)</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
Line 406:
[[wp:Monkey_patch|monkeypatched]] example.
 
<langsyntaxhighlight AutoHotkeylang="autohotkey">point := Object()
point.x := 1
point.y := 0
</syntaxhighlight>
</lang>
 
=={{header|AWK}}==
As usual, arrays are the only data type more complex than a number or a string.<br>
Use quotes around constant strings as element selectors:
<langsyntaxhighlight lang="awk">BEGIN {
p["x"]=10
p["y"]=42
Line 424:
for (i in p) print( i, ":", p[i] )
}</langsyntaxhighlight>
{{out}}
<pre>
Line 434:
=={{header|Axe}}==
Axe does not have language support for custom data structures. However, they can be implemented from scratch using memory directly.
<langsyntaxhighlight lang="axe">Lbl POINT
r₂→{r₁}ʳ
r₃→{r₁+2}ʳ
r₁
Return</langsyntaxhighlight>
 
To initialize a POINT at memory address L₁ with (x, y) = (5, 10):
<syntaxhighlight lang ="axe">POINT(L₁,5,10)</langsyntaxhighlight>
 
The caller must ensure the buffer has enough free space to contain the object (in this case, 4 bytes).
Line 449:
{{works with|PowerBASIC}}
 
<langsyntaxhighlight lang="qb">TYPE Point
x AS INTEGER
y AS INTEGER
END TYPE</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> DIM Point{x%, y%}</langsyntaxhighlight>
 
=={{header|Bracmat}}==
Line 463:
So we go object oriented and create a 'type' Point. We show that <code>x</code> and <code>y</code> are independent by changing the value of <code>x</code> and checking that <code>y</code> didn't change.
Bracmat does not have other typing systems than duck typing. The variable <code>Point</code> is not a class, but an object in its own right. The <code>new$</code> function creates a copy of <code>Point</code>.
<langsyntaxhighlight Bracmatlang="bracmat">( ( Point
= (x=)
(y=)
Line 473:
& 7:?(pt..x)
& out$(!(pt..x) !(pt..y))
);</langsyntaxhighlight>
{{out}}
<pre>3 4
Line 486:
=={{header|C}}==
 
<langsyntaxhighlight lang="c">typedef struct Point
{
int x;
int y;
} Point;</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
 
<langsyntaxhighlight lang="csharp">struct Point
{
public int x, y;
Line 501:
this.y = y;
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">struct Point
{
int x;
int y;
};</langsyntaxhighlight>
 
It is also possible to add a constructor (this allows the use of <tt>Point(x, y)</tt> in expressions):
<langsyntaxhighlight lang="cpp">struct Point
{
int x;
int y;
Point(int ax, int ay): x(ax), y(ax) {}
};</langsyntaxhighlight>
 
Point can also be parametrized on the coordinate type:
<langsyntaxhighlight lang="cpp">template<typename Coordinate> struct point
{
Coordinate x, y;
Line 528:
 
// a point with floating point coordinates
Point<float> point2 = { 1.7, 3.6 };</langsyntaxhighlight>
Of course, a constructor can be added in this case as well.
 
=={{header|Clean}}==
===Record type===
<langsyntaxhighlight lang="clean">:: Point = { x :: Int, y :: Int }</langsyntaxhighlight>
===Parameterized Algebraic type===
<langsyntaxhighlight lang="clean">:: Point a = Point a a // usage: (Point Int)</langsyntaxhighlight>
===Synonym type===
<langsyntaxhighlight lang="clean">:: Point :== (Int, Int)</langsyntaxhighlight>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(defrecord Point [x y])</langsyntaxhighlight>
This defines a datatype with constructor ''Point.'' and accessors '':x'' and '':y'' :
<langsyntaxhighlight lang="clojure">(def p (Point. 0 1))
(assert (= 0 (:x p)))
(assert (= 1 (:y p)))</langsyntaxhighlight>
 
=={{header|CLU}}==
Line 550:
Aside from this, they work the same way.
 
<langsyntaxhighlight lang="clu">% Definitions
point = struct[x, y: int]
mutable_point = record[x, y: int]
Line 556:
% Initialization
p: point := point${x: 10, y: 20}
mp: mutable_point := mutable_point${x: 10, y: 20}</langsyntaxhighlight>
 
The fields can be accessed using the <code>.</code> syntax:
<langsyntaxhighlight lang="clu">foo := p.x
bar := p.y</langsyntaxhighlight>
 
''Record''s, but not ''struct''s, allow updating the fields in the same way.
<langsyntaxhighlight lang="clu">mp.x := 30
mp.y := 40</langsyntaxhighlight>
 
It should be noted that the special forms <code>p.x</code> and <code>mp.x := value</code>
are really only syntactic sugar, they are equivalent to the following method calls:
<langsyntaxhighlight lang="clu">foo := point$get_x(p)
bar := point$get_y(p)</langsyntaxhighlight>
 
<langsyntaxhighlight lang="clu">mutable_point$set_x(mp, 30)
mutable_point$set_y(mp, 40)</langsyntaxhighlight>
 
=={{header|COBOL}}==
A compound data item description is possible in COBOL as a subdivided record:
<lang cobol>
<syntaxhighlight lang="cobol"> DATA DIVISION.
01 Point.
05 x WORKING-STORAGE pic 9(3)SECTION.
05 y 01 pic 9(3)Point.
05 x USAGE IS BINARY-SHORT.
</lang>
05 y USAGE IS BINARY-SHORT.</syntaxhighlight>
Here the record <code>Point</code> has the subdivisions <code>x</code> and <code>y</code>, both of which are signed 16-bit binary integers.
 
=={{header|CoffeeScript}}==
<langsyntaxhighlight lang="coffeescript">
# Lightweight JS objects (with CS sugar).
point =
Line 603 ⟶ 605:
console.log p1.distance_from # [Function]
console.log p1.distance_from p2 # 13
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
 
<langsyntaxhighlight lang="lisp">CL-USER> (defstruct point (x 0) (y 0)) ;If not provided, x or y default to 0
POINT</langsyntaxhighlight>
In addition to defining the ''point'' data type, the defstruct macro also created constructor and accessor functions:
<langsyntaxhighlight lang="lisp">CL-USER> (setf a (make-point)) ;The default constructor using the default values for x and y
#S(POINT :X 0 :Y 0)
CL-USER> (setf b (make-point :x 5.5 :y #C(0 1))) ;Dynamic datatypes are the default
Line 621 ⟶ 623:
3
CL-USER> (point-y b)
3</langsyntaxhighlight>
 
=={{header|Crystal}}==
Crystal's structs work very similarly to objects, but are allocated on the stack instead of the heap, and passed by value instead of by reference. More potential caveats are noted in the [https://crystal-lang.org/reference/syntax_and_semantics/structs.html language reference].
 
<langsyntaxhighlight lang="ruby">struct Point(T)
getter x : T
getter y : T
Line 633 ⟶ 635:
end
 
puts Point(Int32).new 13, 12 #=> Point(Int32)(@x=13, @y=12)</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">void main() {
// A normal POD struct
// (if it's nested and it's not static then it has a hidden
Line 682 ⟶ 684:
static assert(is(p6[0] == int));
static assert(p6[1] == 5);
}</langsyntaxhighlight>
 
=={{header|Delphi}}==
As defined in Types.pas:
 
<langsyntaxhighlight Delphilang="delphi"> TPoint = record
X: Longint;
Y: Longint;
end;
</syntaxhighlight>
</lang>
 
=={{header|Diego}}==
<langsyntaxhighlight lang="diego">use_namespace(rosettacode)_me();
 
add_struct(point)_arg(x,y);
Line 703 ⟶ 705:
with_point(point2)_arg(0.033,👣);
 
reset_namespace[];</langsyntaxhighlight>
 
=={{header|E}}==
 
<langsyntaxhighlight lang="e">def makePoint(x, y) {
def point {
to getX() { return x }
Line 713 ⟶ 715:
}
return point
}</langsyntaxhighlight>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
(lib 'struct)
(struct Point (x y))
Line 729 ⟶ 731:
(Point 3 'albert)
❌ error: #number? : type-check failure : albert → 'Point:y'
</syntaxhighlight>
</lang>
 
=={{header|Ela}}==
Line 735 ⟶ 737:
Ela supports algebraic types:
 
<langsyntaxhighlight lang="ela">type Maybe = None | Some a</langsyntaxhighlight>
 
Except of regular algebraic types, Ela also provides a support for open algebraic types - which can be extended any time with new constructors:
 
<langsyntaxhighlight lang="ela">opentype Several = One | Two | Three
 
//Add new constructor to an existing type
data Several = Four</langsyntaxhighlight>
 
=={{header|Elena}}==
ELENA 6.x:
<lang elena>struct Point
<syntaxhighlight lang="elena">struct Point
{
prop int X : prop;
prop int Y : prop;
constructor new(int x, int y)
Line 756 ⟶ 759:
Y := y
}
}</langsyntaxhighlight>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">iex(1)> defmodule Point do
...(1)> defstruct x: 0, y: 0
...(1)> end
Line 776 ⟶ 779:
10
iex(8)> py
20</langsyntaxhighlight>
 
=={{header|Elm}}==
<langsyntaxhighlight lang="elm">
--Compound Data type can hold multiple independent values
--In Elm data can be compounded using List, Tuple, Record
Line 807 ⟶ 810:
--Each time a new record is generated
--END
</syntaxhighlight>
</lang>
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">
-module(records_test).
-compile(export_all).
Line 821 ⟶ 824:
P2 = P1#point{x=3.0}, % creates a new point record with x set to 3.0, y is copied from P1
io:fwrite("X: ~f, Y: ~f~n",[P2#point.x,P2#point.y]).
</syntaxhighlight>
</lang>
 
=={{header|Euphoria}}==
{{works with|OpenEuphoria}}
<langsyntaxhighlight lang="euphoria">
enum x, y
 
Line 837 ⟶ 840:
printf(1,"x = %d, y = %3.3f\n",point)
printf(1,"x = %s, y = %3.3f\n",point)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 847 ⟶ 850:
=={{header|F_Sharp|F#}}==
See the OCaml section as well. Here we create a list of points and print them out.
<langsyntaxhighlight lang="fsharp">type Point = { x : int; y : int }
 
let points = [
Line 853 ⟶ 856:
{x = 5; y = 5} ]
Seq.iter (fun p -> printfn "%d,%d" p.x p.y) points</langsyntaxhighlight>
 
=={{header|Factor}}==
<syntaxhighlight lang ="factor">TUPLE: point x y ;</langsyntaxhighlight>
 
=={{header|Fantom}}==
 
<langsyntaxhighlight lang="fantom">
// define a class to contain the two fields
// accessors to get/set the field values are automatically generated
Line 881 ⟶ 884:
}
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 892 ⟶ 895:
There is no standard structure syntax in Forth, but it is easy to define words for creating and accessing data structures.
 
<langsyntaxhighlight lang="forth">: pt>x ( point -- x ) ;
: pt>y ( point -- y ) CELL+ ;
: .pt ( point -- ) dup pt>x @ . pt>y @ . ; \ or for this simple structure, 2@ . .
Line 898 ⟶ 901:
create point 6 , 0 ,
7 point pt>y !
.pt \ 6 7</langsyntaxhighlight>
 
{{works with|GNU Forth|0.6.2}}
Some Forths have mechanisms for declaring complex structures. For example, GNU Forth uses this syntax:
 
<langsyntaxhighlight lang="forth">struct
cell% field pt>x
cell% field pt>y
end-struct point%</langsyntaxhighlight>
 
=={{header|Fortran}}==
In ISO Fortran 90 or later, use a TYPE declaration, "constructor" syntax, and field delimiter syntax:
<langsyntaxhighlight lang="fortran">program typedemo
type rational ! Type declaration
integer :: numerator
Line 931 ⟶ 934:
oon_denoms = one_over_n%denominator ! Access denominator field in every
! rational array element & store
end program typedemo ! as integer array</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Type Point
Line 944 ⟶ 947:
Print p.x, p.y
Print p2.x, p2.y
Sleep</langsyntaxhighlight>
 
{{out}}
Line 951 ⟶ 954:
3 4
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
CGRect r = {0, 0, 250, 100}
printf @"x = %.f : y = %.f : width = %.f : height = %.f", r.origin.x, r.origin.y, r.size.width, r.size.height
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
x = 0 : y = 0 : width = 250 : height = 100
 
</pre>
 
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">type point struct {
x, y float64
}
</syntaxhighlight>
</lang>
 
=={{header|Groovy}}==
 
===Declaration===
<langsyntaxhighlight lang="groovy">class Point {
int x
int y
Line 968 ⟶ 985:
Point(int x = 0, int y = 0) { this.x = x; this.y = y }
String toString() { "{x:${x}, y:${y}}" }
}</langsyntaxhighlight>
 
===Instantiation===
=====Direct=====
<langsyntaxhighlight lang="groovy">// Default Construction with explicit property setting:
def p0 = new Point()
assert 0 == p0.x
Line 988 ⟶ 1,005:
def p2 = new Point(36)
assert 36 == p2.x
assert 0 == p2.y</langsyntaxhighlight>
 
=====List-to-argument Substitution=====
There are several ways that a List can be substituted for constructor arguments via "type coercion" (casting).
<langsyntaxhighlight lang="groovy">// Explicit coersion from list with "as" keyword
def p4 = [36, -2] as Point
assert 36 == p4.x
Line 1,010 ⟶ 1,027:
Point p8 = [36]
assert 36 == p8.x
assert 0 == p8.y</langsyntaxhighlight>
 
=====Map-to-property Substitution=====
There are several ways to construct an object using a map (or a comma-separated list of map entries) that substitutes entries for class properties. The process is properly (A) instantiation, followed by (B) property mapping. Because the instantiation is not tied to the mapping, it requires the existence of a no-argument constructor.
<langsyntaxhighlight lang="groovy">// Direct map-based construction
def p3 = new Point([x: 36, y: -2])
assert 36 == p3.x
Line 1,044 ⟶ 1,061:
Point p9 = [y:-2]
assert 0 == p9.x
assert -2 == p9.y</langsyntaxhighlight>
 
=={{header|Haskell}}==
Line 1,084 ⟶ 1,101:
You can make a tuple literal by using a comma-delimited list surrounded by parentheses, without needing to declare the type first:
 
<langsyntaxhighlight lang="haskell">p = (2,3)</langsyntaxhighlight>
 
The type of <code>p</code> is <code>(Int, Int)</code>, using the same comma-delimited list syntax as the literal.
Line 1,099 ⟶ 1,116:
 
=={{header|Icon}} and {{header|Unicon}}==
<syntaxhighlight lang ="icon">record Point(x,y)</langsyntaxhighlight>
 
=={{header|IDL}}==
 
<langsyntaxhighlight lang="idl">point = {x: 6 , y: 0 }
point.y = 7
print, point
;=> { 6 7}</langsyntaxhighlight>
 
=={{header|J}}==
Line 1,112 ⟶ 1,129:
In a "real" J application, points would be represented by arrays of 2 (or N) numbers. None the less, sometimes objects (in the OO sense) are a better representation than arrays, so J supports them:
 
<langsyntaxhighlight lang="j"> NB. Create a "Point" class
coclass'Point'
 
Line 1,128 ⟶ 1,145:
10
Y__P
20</langsyntaxhighlight>
 
=={{header|Jakt}}==
<syntaxhighlight lang="jakt">
struct Point {
x: i64
y: i64
}
 
fn main() {
println("{}", Point(x: 3, y: 4))
}
</syntaxhighlight>
 
=={{header|Java}}==
Starting with Java 14 you can use a record
<syntaxhighlight lang="java">
record Point(int x, int y) { }
</syntaxhighlight>
Usage
<syntaxhighlight lang="java">
Point point = new Point(1, 2);
int x = point.x;
int y = point.y;
</syntaxhighlight>
<br />
Alternately
We use a class:
<langsyntaxhighlight lang="java">public class Point
{
public int x, y;
Line 1,145 ⟶ 1,186:
System.out.println("y = " + point.y );
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
 
<langsyntaxhighlight lang="javascript">//using object literal syntax
var point = {x : 1, y : 2};
 
Line 1,166 ⟶ 1,207:
}
}
point = new Point(1, 2);</langsyntaxhighlight>
 
=={{header|jq}}==
<langsyntaxhighlight lang="jq">{"x":1, "y":2}</langsyntaxhighlight>
 
If the emphasis in the task description is on "type", then an alternative approach would be to include a "type" key, e.g.
<langsyntaxhighlight lang="jq">{"x":1, "y":2, type: "Point"}</langsyntaxhighlight>
 
Using this approach, one can distinguish between objects of type "Point" and those that happen to have keys named "x" and "y".
Line 1,178 ⟶ 1,219:
=={{header|JSON}}==
 
<langsyntaxhighlight lang="json">{"x":1,"y":2}</langsyntaxhighlight>
 
=={{header|Julia}}==
'''Define the type''':
<langsyntaxhighlight lang="julia">struct Point{T<:Real}
x::T
y::T
end</langsyntaxhighlight>
The components of <code>Point</code> can be any sort of real number, though they do have to be of the same type.
 
'''Define a few simple operations for Point''':
<langsyntaxhighlight lang="julia">Base.:(==)(u::Point, v::Point) = u.x == v.x && u.y == v.y
Base.:-(u::Point) = Point(-u.x, -u.y)
Base.:+(u::Point, v::Point) = Point(u.x + v.x, u.y + v.y)
Base.:-(u::Point, v::Point) = u + (-v)</langsyntaxhighlight>
 
'''Have fun''':
<langsyntaxhighlight lang="julia">a, b, c = Point(1, 2), Point(3, 7), Point(2, 4)
@show a b c
@show a + b
Line 1,202 ⟶ 1,243:
@show a + b + c
@show a == b
@show a + a == c</langsyntaxhighlight>
 
{{out}}
Line 1,216 ⟶ 1,257:
 
=={{header|KonsolScript}}==
<langsyntaxhighlight KonsolScriptlang="konsolscript">Var:Create(
Point,
Number x,
Number y
)</langsyntaxhighlight>
 
Instanciate it with...
<langsyntaxhighlight KonsolScriptlang="konsolscript">function main() {
Var:Point point;
}</langsyntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">data class Point(var x: Int, var y: Int)
 
fun main(args: Array<String>) {
Line 1,236 ⟶ 1,277:
p.y = 4
println(p)
}</langsyntaxhighlight>
 
{{out}}
Line 1,245 ⟶ 1,286:
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang="scheme">
1) a pair
{def P {P.new 1 2}}
Line 1,269 ⟶ 1,310:
{A.last {R}}
-> 2
</syntaxhighlight>
</lang>
 
=={{header|Lang}}==
<syntaxhighlight lang="lang">
&Point = {
$x
$y
}
</syntaxhighlight>
 
=={{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:
<langsyntaxhighlight lang="lasso">define Point => type {
parent pair
 
Line 1,286 ⟶ 1,335:
local(point) = Point(33, 42)
#point->x
#point->y</langsyntaxhighlight>
 
{{out}}
Line 1,296 ⟶ 1,345:
Simply define a record in the LFE REPL (can also be used in include files, modules, etc.):
 
<langsyntaxhighlight lang="lisp">
(defrecord point
x
y)
</syntaxhighlight>
</lang>
 
Creating points:
Line 1,342 ⟶ 1,391:
=={{header|Lingo}}==
Point and Vector types are built-in. A custom "MyPoint" type can be implemented like this:
<langsyntaxhighlight lang="lingo">-- parent script "MyPoint"
property x
property y
Line 1,349 ⟶ 1,398:
me.y = py
return me
end</langsyntaxhighlight>
<langsyntaxhighlight lang="lingo">p = script("MyPoint").new(23, 42)
put p.x, p.y
-- 23 42</langsyntaxhighlight>
Construction could also be simplified by using a global wrapper function:
<langsyntaxhighlight lang="lingo">-- in some movie script
on MyPoint (x, y)
return script("MyPoint").new(x, y)
end</langsyntaxhighlight>
<langsyntaxhighlight lang="lingo">p = MyPoint(23, 42)
put p.x, p.y
-- 23 42</langsyntaxhighlight>
 
=={{header|Logo}}==
In Logo, a point is represented by a list of two numbers. For example, this will draw a triangle:
<langsyntaxhighlight lang="logo">setpos [100 100] setpos [100 0] setpos [0 0]
show pos ; [0 0]</langsyntaxhighlight>
Access is via normal list operations like FIRST and BUTFIRST (BF). X is FIRST point, Y is LAST point. For example, a simple drawing program which exits if mouse X is negative:
<langsyntaxhighlight lang="logo">until [(first mousepos) < 0] [ifelse button? [pendown] [penup] setpos mousepos]</langsyntaxhighlight>
 
=={{header|Lua}}==
Line 1,375 ⟶ 1,424:
Lua could use a simple table to store a compound data type Point(x, y):
 
<langsyntaxhighlight lang="lua">
a = {x = 1; y = 2}
b = {x = 3; y = 4}
Line 1,384 ⟶ 1,433:
print(a.x, a.y) --> 1 2
print(c.x, c.y) --> 4 6
</syntaxhighlight>
</lang>
 
==== Prototype Object ====
 
Furthermore, Lua could create a prototype object (OOP class emulation) to represent a compound data type Point(x, y) as the following:
<langsyntaxhighlight lang="lua">
cPoint = {} -- metatable (behaviour table)
function newPoint(x, y) -- constructor
Line 1,399 ⟶ 1,448:
return setmetatable(pointPrototype, cPoint) -- set behaviour and return the pointPrototype
end--newPoint
</syntaxhighlight>
</lang>
 
In the above example, the methods are declared inside the constructor so that they could access the closured values <code>x</code> and <code>y</code> (see usage example). The <code>pointPrototype:type</code> method could be used to extend the original <code>type</code> function available in Lua:
 
<langsyntaxhighlight lang="lua">
local oldtype = type; -- store original type function
function type(v)
Line 1,413 ⟶ 1,462:
end--if vType=="table"
end--type
</syntaxhighlight>
</lang>
 
The usage of metatable <code>cPoint</code> which stores the behavior of the <code>pointPrototype</code> enables additional behaviour to be added to the data type, such as:
 
<langsyntaxhighlight lang="lua">
function cPoint.__add(op1, op2) -- add the x and y components
if type(op1)=="point" and type(op2)=="point" then
Line 1,432 ⟶ 1,481:
end--if type(op1)
end--cPoint.__sub
</syntaxhighlight>
</lang>
 
Usage example:
 
<langsyntaxhighlight lang="lua">
a = newPoint(1, 2)
b = newPoint(3, 4)
Line 1,444 ⟶ 1,493:
print(c:getXY()) --> 4 6
print((a-b):getXY()) --> -2 -2 -- using __sub behaviour
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">Point:= Record(x = 2,y = 4):
 
Point:-x;
Point:-y;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,459 ⟶ 1,508:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Expressions like point[x, y] can be used without defining.
<langsyntaxhighlight lang="mathematica">In[1]:= a = point[2, 3]
 
Out[1]= point[2, 3]
Line 1,469 ⟶ 1,518:
In[3]:= a[[2]] = 4; a
 
Out[3]= point[2, 4]</langsyntaxhighlight>
 
Or you can just define a function.
<langsyntaxhighlight lang="mathematica">p[x] = 2; p[y] = 3;</langsyntaxhighlight>
Data will be stored as down values of the symbol ''p''.
 
=={{header|MATLAB}} / {{header|Octave}}==
 
<langsyntaxhighlight MATLABlang="matlab"> point.x=3;
point.y=4;</langsyntaxhighlight>
Alternatively, coordinates can be also stored as vectors
<langsyntaxhighlight MATLABlang="matlab"> point = [3,4];</langsyntaxhighlight>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">defstruct(point(x, y))$
 
p: new(point)$
Line 1,489 ⟶ 1,538:
q: point(1, 2)$
 
p@x: 5$</langsyntaxhighlight>
 
=={{header|MAXScript}}==
Point is a built-in object type in MAX, so...
<langsyntaxhighlight lang="maxscript">struct myPoint (x, y)
newPoint = myPoint x:3 y:4</langsyntaxhighlight>
In practice however, you'd use MAX's built in Point2 type
<langsyntaxhighlight lang="maxscript">newPoint = Point2 3 4</langsyntaxhighlight>
 
=={{header|MiniScript}}==
<langsyntaxhighlight MiniScriptlang="miniscript">Point = {}
Point.x = 0
Point.y = 0</langsyntaxhighlight>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">TYPE Point = RECORD
x, y : INTEGER
END;</langsyntaxhighlight>
 
Usage:
<langsyntaxhighlight lang="modula2">VAR point : Point;
...
point.x := 12;
point.y := 7;</langsyntaxhighlight>
 
=={{header|Modula-3}}==
<langsyntaxhighlight lang="modula3">TYPE Point = RECORD
x, y: INTEGER;
END;</langsyntaxhighlight>
 
Usage:
Line 1,534 ⟶ 1,583:
=={{header|NetRexx}}==
Like Java, NetRexx uses the <tt>class</tt> instruction to create compound types. Unlike Java; NetRexx provides keywords to automatically generate getters and setters for <tt>class</tt> properties and will automatically generate intermediate methods based on defaults provided in method prototypes.
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 1,556 ⟶ 1,605:
res = 'X='getX()',Y='getY()
return res
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,563 ⟶ 1,612:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">type Point = tuple[x, y: int]
 
var p: Point = (12, 13)
var p2: Point = (x: 100, y: 200)</langsyntaxhighlight>
 
=={{header|Oberon-2}}==
<langsyntaxhighlight lang="oberon2">
MODULE Point;
TYPE
Line 1,590 ⟶ 1,639:
 
END Point.
</syntaxhighlight>
</lang>
 
=={{header|Objeck}}==
Classes are used for compound data types.
<langsyntaxhighlight lang="objeck">
class Point {
@x : Int;
Line 1,630 ⟶ 1,679:
}
}
</syntaxhighlight>
</lang>
 
=={{header|OCaml}}==
Line 1,637 ⟶ 1,686:
See [[wp:Algebraic_data_type|algebraic data type]]. The different options ("Empty", "Leaf", "Node") are called ''constructors'', and is associated with 0 or more arguments with the declared types; multiple arguments are declared with a syntax that looks like a tuple type, but it is not really a tuple.
 
<langsyntaxhighlight lang="ocaml">type tree = Empty
| Leaf of int
| Node of tree * tree
 
let t1 = Node (Leaf 1, Node (Leaf 2, Leaf 3))</langsyntaxhighlight>
 
===Record Type===
 
<langsyntaxhighlight lang="ocaml">type point = { x : int; y : int }</langsyntaxhighlight>
 
How to construct a point:
 
<langsyntaxhighlight lang="ocaml">let p = { x = 4; y = 5 }</langsyntaxhighlight>
 
You can use the dot (".") to access fields.
<langsyntaxhighlight lang="ocaml">p.x (* evaluates to 4 *)</langsyntaxhighlight>
 
Fields can be optionally declared to be mutable:
<langsyntaxhighlight lang="ocaml">type mutable_point = { mutable x2 : int; mutable y2 : int }</langsyntaxhighlight>
 
Then they can be assigned using the assignment operator "<-"
<langsyntaxhighlight lang="ocaml">let p2 = { x2 = 4; y2 = 5 } in
p2.x2 <- 6;
p2 (* evaluates to { x2 = 6; y2 = 5 } *)</langsyntaxhighlight>
 
===Tuple Type===
Line 1,666 ⟶ 1,715:
You can make a tuple literal by using a comma-delimited list, optionally surrounded by parentheses, without needing to declare the type first:
 
<langsyntaxhighlight lang="ocaml">let p = (2,3)</langsyntaxhighlight>
 
The type of <code>p</code> is a product (indicated by <code>*</code>) of the types of the components:
Line 1,676 ⟶ 1,725:
Using a class :
 
<langsyntaxhighlight lang="oforth">Object Class new: Point(x, y)</langsyntaxhighlight>
 
=={{header|ooRexx}}==
ooRexx uses class for compound data types.
<syntaxhighlight lang="oorexx">
<lang ooRexx>
p = .point~new(3,4)
say "x =" p~x
Line 1,692 ⟶ 1,741:
::attribute x
::attribute y
</syntaxhighlight>
</lang>
 
=={{header|OpenEdge/Progress}}==
Line 1,698 ⟶ 1,747:
The temp-table is a in memory database table. So you can query sort and iterate it, but is the data structure that comes closest.
 
<langsyntaxhighlight Progresslang="progress (Openedgeopenedge ABLabl)">def temp-table point
field x as int
field y as int
.</langsyntaxhighlight>
 
Another option would be a simple class.
 
=={{header|OxygenBasic}}==
<langsyntaxhighlight lang="oxygenbasic">
'SHORT FORM
type point float x,y
Line 1,728 ⟶ 1,777:
 
print p.x " " p.y
</syntaxhighlight>
</lang>
 
=={{header|Oz}}==
A point can be represented by using a record value:
<langsyntaxhighlight lang="oz">P = point(x:1 y:2)</langsyntaxhighlight>
 
Now we can access the components by name: P.x and P.y
Often such values are deconstructed by pattern matching:
<langsyntaxhighlight lang="oz">case P of point(x:X y:Y) then
{Show X}
{Show Y}
end</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">point.x=1;
point.y=2;</langsyntaxhighlight>
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">type point = record
x, y: integer;
end;</langsyntaxhighlight>
 
=={{header|Perl}}==
 
===Array===
<langsyntaxhighlight lang="perl">my @point = (3, 8);</langsyntaxhighlight>
 
===Hash===
<langsyntaxhighlight lang="perl">my %point = (
x => 3,
y => 8
);</langsyntaxhighlight>
 
===Class instance===
<langsyntaxhighlight lang="perl">package Point;
 
use strict;
Line 1,770 ⟶ 1,819:
;
 
my $point = Point->new(x => 3, y => 8);</langsyntaxhighlight>
 
=={{header|Phix}}==
===traditional user defined type===
The sequence is a natural compound data type. The following would be the same without the type point and declaring p as a sequence, apart from the run-time error. There would be no difficulty defining point to have a string and two atoms.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">enum</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span>
Line 1,789 ⟶ 1,838:
<span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">x</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span> <span style="color: #000080;font-style:italic;">-- fine</span>
<span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">y</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"string"</span> <span style="color: #000080;font-style:italic;">-- run-time error (not pwa/p2js)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,805 ⟶ 1,854:
{{libheader|Phix/Class}}
You could also use a class (not pwa/p2js)
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #008080;">class</span> <span style="color: #000000;">point</span>
<span style="color: #008080;">public</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span>
Line 1,815 ⟶ 1,864:
<span style="color: #000000;">p</span><span style="color: #0000FF;">.</span><span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span> <span style="color: #000080;font-style:italic;">-- fine</span>
<span style="color: #000000;">p</span><span style="color: #0000FF;">.</span><span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"string"</span> <span style="color: #000080;font-style:italic;">-- run-time error</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,829 ⟶ 1,878:
=={{header|PHP}}==
 
<langsyntaxhighlight lang="php"># Using pack/unpack
$point = pack("ii", 1, 2);
 
Line 1,838 ⟶ 1,887:
list($x,$y) = unpack("ii", $point);
echo $x;
echo $y;</langsyntaxhighlight>
 
<langsyntaxhighlight lang="php"># Using array
$point = array('x' => 1, 'y' => 2);
 
Line 1,847 ⟶ 1,896:
 
# or simply:
echo $point['x'], ' ', $point['y'], "\n";</langsyntaxhighlight>
 
<langsyntaxhighlight lang="php"># Using class
class Point {
function __construct($x, $y) { $this->x = $x; $this->y = $y; }
Line 1,855 ⟶ 1,904:
}
$point = new Point(1, 2);
echo $point; # will call __tostring() in later releases of PHP 5.2; before that, it won't work so good.</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(class +Point)
 
(dm T (X Y)
Line 1,866 ⟶ 1,915:
(setq P (new '(+Point) 3 4))
 
(show P)</langsyntaxhighlight>
{{out}}
<pre>$52717735311266 (+Point)
Line 1,873 ⟶ 1,922:
 
=={{header|Pike}}==
<syntaxhighlight lang="pike">
<lang Pike>
class Point {
int x, y;
Line 1,888 ⟶ 1,937:
write("%d %d\n", point->x, point->y);
}
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,895 ⟶ 1,944:
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
define structure
1 point,
Line 1,902 ⟶ 1,951:
 
 
</syntaxhighlight>
</lang>
 
=={{header|Plain English}}==
<langsyntaxhighlight lang="plainenglish">A cartesian point is a record with an x coord and a y coord.</langsyntaxhighlight>
 
=={{header|Pop11}}==
 
<langsyntaxhighlight lang="pop11">uses objectclass;
define :class Point;
slot x = 0;
slot y = 0;
enddefine;</langsyntaxhighlight>
 
=={{header|PowerShell}}==
{{works with|PowerShell|5}}
<syntaxhighlight lang="powershell">
<lang PowerShell>
class Point {
[Int]$a
Line 1,936 ⟶ 1,985:
$p1.add()
$p2.mul()
</syntaxhighlight>
</lang>
<b>Output:</b>
<pre>
Line 1,946 ⟶ 1,995:
Prolog terms ARE compound data types, there is no need to specifically define a type.
for the purpose of this exercise you could define a rule like so:
<syntaxhighlight lang ="prolog">point(10, 20).</langsyntaxhighlight>
This will create static point that can be called:
<langsyntaxhighlight lang="prolog">?- point(X,Y).
X = 10,
Y = 20.</langsyntaxhighlight>
terms can be passed around as values and can have a complex nested structure of any size, eg:
<langsyntaxhighlight lang="prolog">person_location(person(name(N), age(A)), point(X, Y)).</langsyntaxhighlight>
 
=={{header|PureBasic}}==
 
A basic [http://www.purebasic.com/documentation/reference/structures.html structure] is implemented as;
<langsyntaxhighlight PureBasiclang="purebasic">Structure MyPoint
x.i
y.i
EndStructure</langsyntaxhighlight>
 
=={{header|Python}}==
 
The simplest way it to use a tuple, or a list if it should be mutable:
<langsyntaxhighlight lang="python">X, Y = 0, 1
p = (3, 4)
p = [3, 4]
 
print p[X]</langsyntaxhighlight>
 
If needed, you can use class:
 
<langsyntaxhighlight lang="python">class Point:
def __init__(self, x=0, y=0):
self.x = x
Line 1,979 ⟶ 2,028:
 
p = Point()
print p.x</langsyntaxhighlight>
 
One could also simply instantiate a generic object and "monkeypatch" it:
 
<langsyntaxhighlight lang="python">class MyObject(object): pass
point = MyObject()
point.x, point.y = 0, 1
# objects directly instantiated from "object()" cannot be "monkey patched"
# however this can generally be done to it's subclasses</langsyntaxhighlight>
 
=== Dictionary ===
Mutable. Can add keys (attributes)
<langsyntaxhighlight lang="python">pseudo_object = {'x': 1, 'y': 2}</langsyntaxhighlight>
 
 
Line 1,998 ⟶ 2,047:
As of Python 2.6 one can use the ''collections.namedtuple'' factory to create classes which associate field names with elements of a tuple. This allows one to perform all normal operations on the contained tuples (access by indices or slices, packing and unpacking) while also allowing elements to be accessed by name.
 
<langsyntaxhighlight lang="python">>>> from collections import namedtuple
>>> help(namedtuple)
Help on function namedtuple in module collections:
Line 2,024 ⟶ 2,073:
Point(x=100, y=22)
 
>>></langsyntaxhighlight>
 
=={{header|QB64}}==
<langsyntaxhighlight lang="qb64">Type Point
x As Double
y As Double
Line 2,036 ⟶ 2,085:
p.y = 2.412
 
Print p.x; p.y</langsyntaxhighlight>
{{out}}
<pre> 15.42 2.412</pre>
Line 2,050 ⟶ 2,099:
The word <code>point</code> creates an instance of a nest with two elements, both initialised to zero. The word <code>x</code> specifies the location of the zeroth element within the nest, and the word <code>y</code> specifies the location of the first element within the nest. <code>peek</code> returns the value stored in a specified location, and <code>poke</code> changes the value stored in a specified location, returning the modified nest.
 
<syntaxhighlight lang="quackery">
<lang Quackery>
[ ' [ 0 0 ] ] is point ( --> [ )
 
Line 2,060 ⟶ 2,109:
dup x peek echo cr
99 swap y poke
y peek echo cr</langsyntaxhighlight>
 
{{out}}
Line 2,071 ⟶ 2,120:
The "overkill" solution automates the process of creating new structures with the word <code>struct{</code>, which extends the Quackery compiler to allow the definition of complex compound data structures as follows.
 
<langsyntaxhighlight Quackerylang="quackery"> struct{
item.0
{ item.1.0
Line 2,083 ⟶ 2,132:
} item.1
item.2
}struct mystruct</langsyntaxhighlight>
 
Once defined, the word <code>mystruct</code> will place a new instance of the described structure, with each item initialised to <code>null</code>, on the stack. (The behaviour of <code>null</code> is to place a reference to itself on the stack, as a convenience for debugging, and to allow code to identify elements within the structure that have not had a value assigned to them.)
Line 2,091 ⟶ 2,140:
Names following a <code>}</code> within the definition of a struct (e.g. <code>} item.1.2</code>) return a path to the compound data structure preceding it within the structure. In the example, <code>item.1.2</code> returns the path to <code>{ item.1.2.0 item.1.2.1 item.1.2.2 item.1.2.3 }</code>
 
<langsyntaxhighlight Quackerylang="quackery"> mystruct ( create new instance of a mystruct )
dup echo cr ( this is what it looks like )
789 swap item.1.3 {poke} ( change one of the items )
dup echo cr ( this is what it looks like now )
item.1.3 {peek} echo cr ( retrieve the specified item )
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,106 ⟶ 2,155:
The words <code>{peek}</code>, <code>{poke}</code>, <code>null</code>, and the building word (i.e. compiler extension) <code>struct{</code> defined:
 
<syntaxhighlight lang="text"> [ witheach peek ] is {peek} ( { p --> x )
 
[ dip dup
Line 2,213 ⟶ 2,262:
{}.name again ]
{}.struct release
{}.path release ] builds struct{ ( [ $ --> [ $ )</langsyntaxhighlight>
 
Finally we use <code>struct{</code> etc. to fulfil the requirements go the task.
 
<langsyntaxhighlight Quackerylang="quackery"> struct{ x y }struct point
point
dup x {peek} echo cr
99 swap y {poke}
y {peek} echo cr</langsyntaxhighlight>
 
{{out}}
Line 2,231 ⟶ 2,280:
=={{header|R}}==
R uses the list data type for compound data.
<langsyntaxhighlight Rlang="r">mypoint <- list(x=3.4, y=6.7)
# $x
# [1] 3.4
Line 2,250 ⟶ 2,299:
# [1] 1
# $d$f
# [1] TRUE</langsyntaxhighlight>
 
=={{header|Racket}}==
Line 2,256 ⟶ 2,305:
The most common method uses structures (similar to records):
 
<langsyntaxhighlight lang="racket">
#lang racket
(struct point (x y))
</syntaxhighlight>
</lang>
 
Alternatively, you can define a class:
 
<langsyntaxhighlight lang="racket">
#lang racket
(define point% ; classes are suffixed with % by convention
Line 2,269 ⟶ 2,318:
(super-new)
(init-field x y)))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 2,276 ⟶ 2,325:
 
===Array===
<syntaxhighlight lang="raku" perl6line>my @point = 3, 8;
 
my Int @point = 3, 8; # or constrain to integer elements</langsyntaxhighlight>
 
===Hash===
<syntaxhighlight lang="raku" perl6line>my %point = x => 3, y => 8;
 
my Int %point = x => 3, y => 8; # or constrain the hash to have integer values</langsyntaxhighlight>
 
===Class instance===
<syntaxhighlight lang="raku" perl6line>class Point { has Real ($.x is rw; has, $.y) is rw; }
my Point $point .= new(: x => 3, y => 8);</langsyntaxhighlight>
 
===[http://design.raku.org/S32/Containers.html#Set Set]===
<syntaxhighlight lang="raku" perl6line>my $s1 = set <a b c d>; # order is not preserved
my $s2 = set <c d e f>;
say $s1 (&) $s2; # OUTPUT«set(c, e)»
say $s1 ∩ $s2; # we also do Unicode</langsyntaxhighlight>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">x= -4.9
y= 1.7
 
point=x y</langsyntaxhighlight>
:: ---or---
<langsyntaxhighlight lang="rexx">x= -4.1
y= 1/4e21
 
Line 2,308 ⟶ 2,357:
bpoint=point
 
gpoint=5.6 7.3e-12</langsyntaxhighlight>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
see new point {x=10 y=20} class point x y
</syntaxhighlight>
</lang>
Output
<langsyntaxhighlight lang="ring">
x: 10.000000
y: 20.000000
</syntaxhighlight>
</lang>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">Point = Struct.new(:x,:y)
pt = Point.new(6,7)
puts pt.x #=> 6
Line 2,335 ⟶ 2,384:
pt.each_pair{|member, value| puts "#{member} : #{value}"}
#=> x : 2
#=> y : 5</langsyntaxhighlight>
 
=={{header|Rust}}==
Line 2,342 ⟶ 2,391:
 
====C-like struct====
<langsyntaxhighlight lang="rust"> // Defines a generic struct where x and y can be of any type T
struct Point<T> {
x: T,
Line 2,350 ⟶ 2,399:
let p = Point { x: 1.0, y: 2.5 }; // p is of type Point<f64>
println!("{}, {}", p.x, p.y);
} </langsyntaxhighlight>
 
====Tuple struct====
These are basically just named tuples.
<langsyntaxhighlight lang="rust">struct Point<T>(T, T);
fn main() {
let p = Point(1.0, 2.5);
println!("{},{}", p.0, p.1);
}</langsyntaxhighlight>
===Tuples===
<langsyntaxhighlight lang="rust"> fn main() {
let p = (0.0, 2.4);
println!("{},{}", p.0, p.1);
}</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">case class Point(x: Int = 0, y: Int = 0)
 
val p = Point(1, 2)
println(p.y) //=> 2</langsyntaxhighlight>
 
=={{header|Scheme}}==
Using [http://srfi.schemers.org/srfi-9/srfi-9.html SRFI 9]:
<langsyntaxhighlight lang="scheme">(define-record-type point
(make-point x y)
point?
(x point-x)
(y point-y))</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">const type: Point is new struct
var integer: x is 0;
var integer: y is 0;
end struct;</langsyntaxhighlight>
 
=={{header|Shen}}==
<langsyntaxhighlight lang="shen">(datatype point
X : number; Y : number;
====================
[point X Y] : point;)</langsyntaxhighlight>
Pairs (distinct from cons cells) are also supported, in which case a point would be denoted by (number * number):
<langsyntaxhighlight lang="shen">(2+) (@p 1 2)
(@p 1 2) : (number * number)</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">struct Point {x, y};
var point = Point(1, 2);
say point.y; #=> 2</langsyntaxhighlight>
 
=={{header|SIMPOL}}==
The <code>point</code> type is pre-defined in [SIMPOL], so we will call this mypoint.
 
<langsyntaxhighlight lang="simpol">type mypoint
embed
integer x
integer y
end type</langsyntaxhighlight>
 
The <code>embed</code> keyword is used here as a toggle to indicate that all following properties are embedded in the type. The other toggle is <code>reference</code>, which only places a reference to an object in the type, but the reference assigned before the property can be used. These keywords can also be placed on the same line, but then they only apply to that line of the type definition.
Line 2,412 ⟶ 2,461:
A type in [SIMPOL] can be just a container of values and other structures, but it can also include methods. These are implemented outside the type definition, but must be part of the same compiled unit.
 
<langsyntaxhighlight lang="simpol">type mypoint
embed
integer x
Line 2,421 ⟶ 2,470:
me.x = x
me.y = y
end function me</langsyntaxhighlight>
 
=={{header|SNOBOL4}}==
 
<langsyntaxhighlight lang="snobol"> data('point(x,y)')
p1 = point(10,20)
p2 = point(10,40)
output = "Point 1 (" x(p1) "," y(p1) ")"
output = "Point 2 (" x(p2) "," y(p2) ")"
end</langsyntaxhighlight>
 
=={{header|Standard ML}}==
Line 2,437 ⟶ 2,486:
See [[wp:Algebraic_data_type|algebraic data type]]. The different options ("Empty", "Leaf", "Node") are called ''constructors'', and is associated with 0 or 1 arguments with the declared types; multiple arguments are handled with tuples.
 
<langsyntaxhighlight lang="sml">datatype tree = Empty
| Leaf of int
| Node of tree * tree
 
val t1 = Node (Leaf 1, Node (Leaf 2, Leaf 3))</langsyntaxhighlight>
 
===Tuple Type===
Line 2,447 ⟶ 2,496:
You can make a tuple literal by using a comma-delimited list surrounded by parentheses, without needing to declare the type first:
 
<langsyntaxhighlight lang="sml">val p = (2,3)</langsyntaxhighlight>
 
The type of <code>p</code> is a product (indicated by <code>*</code>) of the types of the components:
Line 2,464 ⟶ 2,513:
You can make a record literal by using a comma-delimited list of <code>key = value</code> pairs surrounded by curly braces, without needing to declare the type first:
 
<langsyntaxhighlight lang="sml">val p = { x = 4, y = 5 }</langsyntaxhighlight>
 
The type of <code>p</code> is a comma-delimited list of <code>key:type</code> pairs of the types of the fields:
Line 2,478 ⟶ 2,527:
See '''[https://www.stata.com/help.cgi?m2_struct struct]''' in Stata help.
 
<langsyntaxhighlight lang="stata">mata
struct Point {
real scalar x, y
Line 2,493 ⟶ 2,542:
test()
30
end</langsyntaxhighlight>
 
=={{header|Swift}}==
<langsyntaxhighlight Swiftlang="swift">// Structure
struct Point {
var x:Int
Line 2,514 ⟶ 2,563:
self.y = y
}
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
This can be done using an associative array:
<langsyntaxhighlight lang="tcl">array set point {x 4 y 5}
set point(y) 7
puts "Point is {$point(x),$point(y)}"
# => Point is {4,7}</langsyntaxhighlight>
Or a dictionary:
{{works with|Tcl|8.5}}
<langsyntaxhighlight lang="tcl">set point [dict create x 4 y 5]
dict set point y 7
puts "Point is {[dict get $point x],[dict get $point y]}"</langsyntaxhighlight>
Or an object:
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">oo::class create Point {
variable x y
constructor {X Y} {set x $X;set y $Y}
Line 2,538 ⟶ 2,587:
Point create point 4 5
point y 7
puts "Point is [point show]"</langsyntaxhighlight>
 
=={{header|TI-89 BASIC}}==
 
TI-89 BASIC does not have user-defined data structures. The specific example of a point is best handled by using the built-in vectors or complex numbers.
 
=={{header|Transd}}==
<syntaxhighlight lang="Scheme">#lang transd
 
// If the Point type needs encapsulation and/or methods, it should be
// implemented as class. Otherwise, the named tuple will do.
 
class Point: {
x: Double(), y: Double(),
@init: (λ _x Double() _y Double() (= x _x) (= y _y)),
@to-String: (λ ss StringStream() (textout to: ss
"Point( x: " x "; y: " y " )"))
// ... other methods can be defined here ...
}
 
MainModule: {
Point2: typealias(Tuple<Double Double>()),
_start: (λ
(with pt Point(2.5 3.7)
(lout "Class: " pt)
)
(with pt Point2(2.5 3.7)
(lout "\nNamed tuple: " pt)
)
)
}</syntaxhighlight>
{{out}}
<pre>
Class: Point( x: 2.5; y: 3.7 )
 
Named tuple: [[2.5, 3.7]]
</pre>
 
=={{header|TXR}}==
Line 2,548 ⟶ 2,629:
In TXR Lisp, a structure type can be created:
 
<langsyntaxhighlight lang="txrlisp">(defstruct point nil (x 0) (y 0))</langsyntaxhighlight>
 
If it is okay for the coordinates to be initialized to <tt>nil</tt>, it can be condensed to:
 
<langsyntaxhighlight lang="txrlisp">(defstruct point nil x y)</langsyntaxhighlight>
 
The <tt>nil</tt> denotes that a <tt>point</tt> has no supertype: it doesn't inherit from anything.
Line 2,558 ⟶ 2,639:
This structure type can then be instantiated using the <tt>new</tt> macro (not the only way):
 
<langsyntaxhighlight lang="txrlisp">(new point) ;; -> #S(point x 0 y 0)
(new point x 1) ;; -> #S(point x 1 y 0)
(new point x 1 y 1) ;; -> #S(point x 1 y 1)</langsyntaxhighlight>
 
A structure can support optional by-order-of-arguments ("boa") construction by providing a "boa constructor". The <tt>defstruct</tt> syntactic sugar does this if a function-like syntax is used in place of the structure name:
 
<langsyntaxhighlight lang="txrlisp">(defstruct (point x y) nil (x 0) (y 0))</langsyntaxhighlight>
 
The existing construction methods continue to work, but in addition, this is now possible:
 
<langsyntaxhighlight lang="txrlisp">(new (point 3 4)) -> #S(point x 3 y 4)</langsyntaxhighlight>
 
Slot access syntax is supported. If variable <tt>p</tt> holds a point, then <tt>p.x</tt> designates the <tt>x</tt> slot, as a syntactic place which can be accessed and stored:
 
<langsyntaxhighlight lang="txrlisp">(defun displace-point-destructively (p delta)
(inc p.x delta.x)
(inc p.y delta.y))</langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
{{works with|ksh93}}
ksh93 allows you to define new compound types with the <tt>typeset -T</tt> command.
<langsyntaxhighlight lang="bash">typeset -T Point=(
typeset x
typeset y
Line 2,589 ⟶ 2,670:
echo ${p.x} ${p.y}
Point q=(x=3 y=4)
echo ${q.x} ${q.y}</langsyntaxhighlight>
{{out}}
<pre>( x=1 y=2 )
Line 2,596 ⟶ 2,677:
 
You can also declare compound variables "on the fly" without using a defined type:
<langsyntaxhighlight lang="bash">point=()
point.x=5
point.y=6
echo $point
echo ${point.x} ${point.y}</langsyntaxhighlight>
{{out}}
<pre>( x=5 y=6 )
Line 2,607 ⟶ 2,688:
=={{header|Ursala}}==
A record type with two untyped fields named <code>x</code> and <code>y</code> can be declared like this.
<syntaxhighlight lang Ursala="ursala">point :: x y</langsyntaxhighlight>
A constant instance of the record can be declared like this.
<langsyntaxhighlight Ursalalang="ursala">p = point[x: 'foo',y: 'bar']</langsyntaxhighlight>
A function returning a value of this type can be defined like this,
<langsyntaxhighlight Ursalalang="ursala">f = point$[x: g,y: h]</langsyntaxhighlight>
where <code>g</code> and <code>h</code> are functions. Then <code>f(p)</code> would evaluate to
<code>point[x: g(p),y: h(p)]</code> for a given argument <code>p</code>. Accessing the fields of
a record can be done like this.
<langsyntaxhighlight Ursalalang="ursala">t = ~x p
u = ~y p</langsyntaxhighlight>
where <code>p</code> is any expression of the defined type. A real application wouldn't be written
this way because pairs of values <code>(x,y)</code> are a common idiom.
 
=={{header|Vala}}==
<langsyntaxhighlight lang="vala">struct Point {
int x;
int y;
}</langsyntaxhighlight>
 
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">Type point
x As Integer
y As Integer
End Type</langsyntaxhighlight>
 
=={{header|Vim Script}}==
One cannot create new data types in Vim Script. A point could be represented by a dictionary:
 
<langsyntaxhighlight lang="vim">function MakePoint(x, y) " 'Constructor'
return {"x": a:x, "y": a:y}
endfunction
Line 2,643 ⟶ 2,724:
 
echon "Point 1: x = " p1.x ", y = " p1.y "\n"
echon "Point 2: x = " p2.x ", y = " p2.y "\n"</langsyntaxhighlight>
 
{{Out}}
Line 2,654 ⟶ 2,735:
 
A simple structure with two public, mutable fields:
<langsyntaxhighlight lang="vbnet">Structure Point
Public X, Y As Integer
End Structure</langsyntaxhighlight>
 
=== Immutable Structures ===
Line 2,668 ⟶ 2,749:
Below is the same <code>Point</code> as above, except with an immutable API.
 
<langsyntaxhighlight lang="vbnet">Structure ImmutablePoint
ReadOnly Property X As Integer
ReadOnly Property Y As Integer
Line 2,676 ⟶ 2,757:
Me.Y = y
End Sub
End Structure</langsyntaxhighlight>
 
=={{header|V (Vlang)}}==
Vlang also supports embedding structs into other structs and assigning methods to structs.
<syntaxhighlight lang="v (vlang)">struct Point {
x int
y int
Line 2,703 ⟶ 2,784:
assert p.x == 30
println("Show the struct again after change:\n $p")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,720 ⟶ 2,801:
 
=={{header|Wren}}==
<langsyntaxhighlight ecmascriptlang="wren">class Point {
construct new(x, y) {
_x = x
Line 2,742 ⟶ 2,823:
p.y = 3
// print without using the toString method
System.printAll(["(", p.x, ", ", p.y, ")"])</langsyntaxhighlight>
 
{{out}}
Line 2,755 ⟶ 2,836:
===Attributes===
Attributes are often used for simple values. This is how a point might be represented in SVG, for example.
<langsyntaxhighlight lang="xml"><point x="20" y="30"/>
 
<!-- context is a point node. The '@' prefix selects named attributes of the current node. -->
<fo:block>Point = <xsl:value-of select="@x"/>, <xsl:value-of select="@y"/></fo:block></langsyntaxhighlight>
 
===Children===
More complex, multivariate, and nested data structures can be represented using child nodes.
<langsyntaxhighlight lang="xml"><circle>
<point>
<x>20</x>
Line 2,770 ⟶ 2,851:
</circle>
 
<!-- context is a circle node. Children are accessed using a path-like notation (hence the name "XPath"). --></langsyntaxhighlight>
<fo:block>Circle center = <xsl:value-of select="point/x"/>, <xsl:value-of select="point/y"/></fo:block>
 
=={{header|Z80 Assembly}}==
We'll declare the following C struct:
<syntaxhighlight lang="c">struct Point{
char x;
char y;
}</syntaxhighlight>
 
and then execute the following C code as Z80 Assembly below.
<syntaxhighlight lang="c">struct Point myPoint;
myPoint.x = 3;
myPoint.y = 5;</syntaxhighlight>
 
<syntaxhighlight lang="z80">;I'm arbitrarily choosing &1100 as the memory location of our Point variable.
ld hl,&1100
ld (hl),3
inc hl
ld (hl),5
ret</syntaxhighlight>
 
=={{header|zkl}}==
The OO solution:
<langsyntaxhighlight lang="zkl">class Point{ var x,y;
fcn init(x,y){self.x=x.toFloat(); self.y=y.toFloat(); }
fcn toString{ "P(%f,%f)".fmt(x,y) }
Line 2,781 ⟶ 2,881:
//... __opEQ == etc
}
Point(1,2).println() //-->P(1.000000,2.000000)</langsyntaxhighlight>
which can be pretty heavy weight. [read only] lists can work just as well:
<langsyntaxhighlight lang="zkl">point:=T(1,2); points:=T( T(1,2), L(3,4) )</langsyntaxhighlight>
 
{{omit from|bc}}
Line 2,789 ⟶ 2,889:
 
=={{header|zonnon}}==
<langsyntaxhighlight lang="zonnon">
{ref,public} (* class *)
Point = object(ord,abs: integer)
Line 2,813 ⟶ 2,913:
self.y := abs;
end Point;
</syntaxhighlight>
</lang>
9,476

edits