Compound data type: Difference between revisions
Content added Content deleted
m (→{{header|J}}: Add lang tags) |
m (Fixed lang tags.) |
||
Line 5: | Line 5: | ||
=={{header|ActionScript}}== |
=={{header|ActionScript}}== |
||
<lang actionscript> |
<lang actionscript>package |
||
package |
|||
{ |
{ |
||
public class Point |
public class Point |
||
Line 19: | Line 18: | ||
} |
} |
||
} |
} |
||
⚫ | |||
} |
|||
⚫ | |||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
Line 54: | Line 52: | ||
===Tagged Type=== |
===Tagged Type=== |
||
ALGOL 68 has only tagged-union/discriminants. And the tagging was strictly done by the ''type'' (MODE) of the members. |
ALGOL 68 has only tagged-union/discriminants. And the tagging was strictly done by the ''type'' (MODE) of the members. |
||
< |
<lang algol68>MODE UNIONX = UNION( |
||
STRUCT(REAL r, INT i), |
STRUCT(REAL r, INT i), |
||
INT, |
INT, |
||
Line 61: | Line 59: | ||
STRUCT(REAL rr), |
STRUCT(REAL rr), |
||
STRUCT([]REAL r) |
STRUCT([]REAL r) |
||
);</ |
);</lang> |
||
To extract the apropriate member of a UNION a '''conformity-clause''' has to be used. |
To extract the apropriate member of a UNION a '''conformity-clause''' has to be used. |
||
⚫ | |||
<pre> |
|||
⚫ | |||
CASE data IN |
CASE data IN |
||
(INT i): printf(($"r: "gl$,i)), |
(INT i): printf(($"r: "gl$,i)), |
||
Line 72: | Line 69: | ||
OUT |
OUT |
||
printf($"Other cases"l$) |
printf($"Other cases"l$) |
||
ESAC; |
ESAC;</lang> |
||
The '''conformity-clause''' does mean that ALGOL 68 avoids the need for |
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, |
[[duck typing]], but it also makes the tagged-union kinda tough to use, |
||
Line 79: | Line 76: | ||
ALGOL 68 record types are not extensible through inheritance but they |
ALGOL 68 record types are not extensible through inheritance but they |
||
may be part of a larger STRUCT composition. |
may be part of a larger STRUCT composition. |
||
< |
<lang algol68>MODE POINT = STRUCT( |
||
INT x, |
INT x, |
||
INT y |
INT y |
||
);</ |
);</lang> |
||
====Parameterized Types==== |
====Parameterized Types==== |
||
An ALGOL 68 record type can contain a tagged-union/discriminant. The |
An ALGOL 68 record type can contain a tagged-union/discriminant. The |
||
tagged-union/discriminant is used to choose between internal structural |
tagged-union/discriminant is used to choose between internal structural |
||
representations. |
representations. |
||
< |
<lang algol68>MODE PERSON = STRUCT( |
||
STRING name, |
STRING name, |
||
REAL age, |
REAL age, |
||
Line 95: | Line 92: | ||
VOID |
VOID |
||
) gender details |
) gender details |
||
);</ |
);</lang> |
||
In this case every PERSON will have the attributes of gender details, name, age, |
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. |
and weight. A PERSON may or may not have a beard. The sex is implied by the tagging. |
||
Line 115: | Line 112: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
As usual, arrays are the only data type more complex than a number or a string. Having to use quotes around constant strings as element selectors: |
As usual, arrays are the only data type more complex than a number or a string. Having to use quotes around constant strings as element selectors: |
||
<lang awk> |
<lang awk>p["x"]=10 |
||
p["y"]=42</lang> |
|||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
Line 163: | Line 160: | ||
Of course, a constructor can be added in this case as well. |
Of course, a constructor can be added in this case as well. |
||
=={{header|C sharp|C |
=={{header|C sharp|C#}}== |
||
<lang csharp>struct Point |
<lang csharp>struct Point |
||
Line 176: | Line 173: | ||
=={{header|Clean}}== |
=={{header|Clean}}== |
||
===Record type=== |
===Record type=== |
||
<lang clean>:: Point = { x :: Int, y :: Int }</lang> |
|||
===Parameterized Algebraic type=== |
===Parameterized Algebraic type=== |
||
<lang clean>:: Point a = Point a a // usage: (Point Int)</lang> |
|||
===Synonym type=== |
===Synonym type=== |
||
<lang clean>:: Point :== (Int, Int)</lang> |
|||
Line 186: | Line 183: | ||
A struct, it can be inizialized with a Point(x, y) syntax: |
A struct, it can be inizialized with a Point(x, y) syntax: |
||
<lang d>struct Point { int x, y; }</lang> |
|||
It can also be parametrized on the coordinate type: |
It can also be parametrized on the coordinate type: |
||
<lang d>struct Point(T) { T x, y; } |
|||
// A point with integer coordinates |
|||
auto p1 = Point!(int)(3, 5); |
|||
// a point with floating point coordinates |
|||
auto p1 = Point!(float)(3, 5);</lang> |
|||
There are also other ways to initialize them. The D language also supports tuples. |
There are also other ways to initialize them. The D language also supports tuples. |
||
Line 206: | Line 203: | ||
=={{header|E}}== |
=={{header|E}}== |
||
<lang e>def makePoint(x, y) { |
|||
def point { |
|||
to getX() { return x } |
|||
to getY() { return y } |
|||
} |
|||
return point |
|||
⚫ | |||
} |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
There is no standard structure syntax in Forth, but it is easy to define words for creating and accessing data structures. |
There is no standard structure syntax in Forth, but it is easy to define words for creating and accessing data structures. |
||
<lang forth>: pt>x ( point -- x ) ; |
|||
: pt>y ( point -- y ) CELL+ ; |
|||
: .pt ( point -- ) dup pt>x @ . pt>y @ . ; \ or for this simple structure, 2@ . . |
|||
create point 6 , 0 , |
|||
7 point pt>y ! |
|||
.pt \ 6 7</lang> |
|||
{{works with|GNU Forth|0.6.2}} |
{{works with|GNU Forth|0.6.2}} |
||
Some Forths have mechanisms for declaring complex structures. For example, GNU Forth uses this syntax: |
Some Forths have mechanisms for declaring complex structures. For example, GNU Forth uses this syntax: |
||
<lang forth>struct |
|||
cell% field pt>x |
|||
cell% field pt>y |
|||
end-struct point%</lang> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
In ISO Fortran 90 or later, use a TYPE declaration, "constructor" syntax, and field delimiter syntax: |
In ISO Fortran 90 or later, use a TYPE declaration, "constructor" syntax, and field delimiter syntax: |
||
<lang fortran> |
<lang fortran>program typedemo |
||
type rational ! Type declaration |
|||
integer :: numerator |
|||
integer :: denominator |
|||
end type rational |
|||
type( rational ), parameter :: zero = rational( 0, 1 ) ! Variables initialized |
|||
type( rational ), parameter :: one = rational( 1, 1 ) ! by constructor syntax |
|||
type( rational ), parameter :: half = rational( 1, 2 ) |
|||
integer :: n, halfd, halfn |
|||
type( rational ) :: & |
|||
one_over_n(20) = (/ (rational( 1, n ), n = 1, 20) /) ! Array initialized with |
|||
! constructor inside |
|||
! implied-do array initializer |
|||
integer :: oon_denoms(20) |
|||
halfd = half%denominator ! field access with "%" delimiter |
|||
halfn = half%numerator |
|||
oon_denoms = one_over_n%denominator ! Access denominator field in every |
|||
! rational array element & store |
|||
end program typedemo ! as integer array</lang> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
See the OCaml section as well. Here we create a list of points and print them out. |
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 = [ |
let points = [ |
||
Line 266: | Line 263: | ||
{x = 5; y = 5} ] |
{x = 5; y = 5} ] |
||
Seq.iter (fun p -> printfn "%d,%d" p.x p.y) points</ |
Seq.iter (fun p -> printfn "%d,%d" p.x p.y) points</lang> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Line 321: | Line 318: | ||
=={{header|IDL}}== |
=={{header|IDL}}== |
||
<lang idl>point = {x: 6 , y: 0 } |
|||
point.y = 7 |
|||
print, point |
|||
;=> { 6 7}</lang> |
|||
Line 347: | Line 344: | ||
10 |
10 |
||
Y__P |
Y__P |
||
20 |
20</lang> |
||
⚫ | |||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 375: | Line 371: | ||
=={{header|JSON}}== |
=={{header|JSON}}== |
||
<lang json>var point = { |
|||
x:1, |
|||
y:2 |
|||
⚫ | |||
}; |
|||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
In Logo, a point is represented by a list of two numbers. For example, this will draw a triangle: |
In Logo, a point is represented by a list of two numbers. For example, this will draw a triangle: |
||
<lang logo>setpos [100 100] setpos [100 0] setpos [0 0] |
|||
show pos ; [0 0]</lang> |
|||
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: |
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: |
||
<lang logo>until [(first mousepos) < 0] [ifelse button? [pendown] [penup] setpos mousepos]</lang> |
|||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
Point is a built-in object type in MAX, so... |
Point is a built-in object type in MAX, so... |
||
<lang maxscript>struct myPoint (x, y) |
|||
newPoint = myPoint x:3 y:4</lang> |
|||
In practice however, you'd use MAX's built in Point2 type |
In practice however, you'd use MAX's built in Point2 type |
||
<lang maxscript>newPoint = Point2 3 4</lang> |
|||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
Line 456: | Line 452: | ||
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. |
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. |
||
<lang openedge>def temp-table point |
|||
field x as int |
|||
field y as int |
|||
.</lang> |
|||
Another option would be a simple class. |
Another option would be a simple class. |
||
Line 508: | Line 504: | ||
=={{header|Pop11}}== |
=={{header|Pop11}}== |
||
<lang pop11>uses objectclass; |
|||
define :class Point; |
|||
slot x = 0; |
|||
slot y = 0; |
|||
enddefine;</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 535: | Line 531: | ||
One could also simply instantiate a generic object and "monkeypatch" it: |
One could also simply instantiate a generic object and "monkeypatch" it: |
||
<lang python> |
<lang python>class MyObject(object): pass |
||
class MyObject(object): pass |
|||
point = MyObject() |
point = MyObject() |
||
point.x, point.y = 0, 1 |
point.x, point.y = 0, 1 |
||
# objects directly instantiated from "object()" cannot be "monkey patched" |
# objects directly instantiated from "object()" cannot be "monkey patched" |
||
# however this can generally be done to it's subclasses |
# however this can generally be done to it's subclasses</lang> |
||
⚫ | |||
=== Named Tuples === |
=== Named Tuples === |
||
Line 573: | Line 567: | ||
Point(x=100, y=22) |
Point(x=100, y=22) |
||
>>> |
>>></lang> |
||
=={{header|R}}== |
=={{header|R}}== |
||
R uses the list data type for compound data. |
R uses the list data type for compound data. |
||
<lang R> |
<lang R>mypoint <- list(x=3.4, y=6.7) |
||
⚫ | |||
mypoint <- list(x=3.4, y=6.7) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
</lang> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Line 676: | Line 667: | ||
This shows a structure in its simpest form. |
This shows a structure in its simpest form. |
||
<lang vbnet>Structure Simple_Point |
|||
Public X, Y As Integer |
|||
End Structure</lang> |
|||
=== Immutable Structures === |
=== Immutable Structures === |
||
Line 684: | Line 675: | ||
In Visual Basic, mutable strucutures are difficult to use properly and should only be used when performance measurements warrant it. The rest of the time, immutable structures should be used. Below is the same structure seen before, but in an immutable form. |
In Visual Basic, mutable strucutures are difficult to use properly and should only be used when performance measurements warrant it. The rest of the time, immutable structures should be used. Below is the same structure seen before, but in an immutable form. |
||
<lang vbnet>Structure Immutable_Point |
|||
Private m_X As Integer |
|||
Private m_Y As Integer |
|||
Public Sub New(ByVal x As Integer, ByVal y As Integer) |
|||
m_X = x |
|||
m_Y = y |
|||
End Sub |
|||
Public ReadOnly Property X() As Integer |
|||
Get |
|||
Return m_X |
|||
End Get |
|||
End Property |
|||
Public ReadOnly Property Y() As Integer |
|||
Get |
|||
Return m_Y |
|||
End Get |
|||
End Property |
|||
End Structure</lang> |
|||
Line 713: | Line 704: | ||
===Attributes=== |
===Attributes=== |
||
Attributes are often used for simple values. This is how a point might be represented in SVG, for example. |
Attributes are often used for simple values. This is how a point might be represented in SVG, for example. |
||
<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></lang> |
|||
===Children=== |
===Children=== |
||
More complex, multivariate, and nested data structures can be represented using child nodes. |
More complex, multivariate, and nested data structures can be represented using child nodes. |
||
<lang xml><circle> |
|||
<point> |
|||
<x>20</x> |
|||
<y>30</y> |
|||
</point> |
|||
<radius>10</radius> |
|||
</circle> |
|||
<!-- context is a circle node. Children are accessed using a path-like notation (hence the name "XPath"). --></lang> |
|||
<fo:block>Circle center = <xsl:value-of select="point/x"/>, <xsl:value-of select="point/y"/></fo:block> |
<fo:block>Circle center = <xsl:value-of select="point/x"/>, <xsl:value-of select="point/y"/></fo:block> |