Compound data type: Difference between revisions

Content added Content deleted
(Add Seed7 example)
(→‎{{header|Groovy}}: new solution)
Line 376:
fmt.Println(point{3, 4})
}</lang>
 
=={{header|Groovy}}==
 
===Declaration===
<lang groovy>class Point {
int x
int y
// Default values make this a 0-, 1-, and 2-argument constructor
Point(int x = 0, int y = 0) { this.x = x; this.y = y }
String toString() { "{x:${x}, y:${y}}" }
}</lang>
 
===Instantiation===
=====Direct=====
<lang groovy>// Default Construction with explicit property setting:
def p0 = new Point()
assert 0 == p0.x
assert 0 == p0.y
p0.x = 36
p0.y = -2
assert 36 == p0.x
assert -2 == p0.y
 
// Direct Construction:
def p1 = new Point(36, -2)
assert 36 == p1.x
assert -2 == p1.y
 
def p2 = new Point(36)
assert 36 == p2.x
assert 0 == p2.y
 
p2 = new Point(x:36)
assert 36 == p2.x
assert 0 == p2.y
 
p2 = new Point(y:-2)
assert 0 == p2.x
assert -2 == p2.y</lang>
 
=====List-to-argument Substitution=====
There are several ways that a List can be substituted for constructor arguments.
<lang groovy>// Explicit coersion from list with "as" keyword
def p4 = [36, -2] as Point
assert 36 == p4.x
assert -2 == p4.y
 
// Implicit coercion from list (by type of variable)
Point p6 = [36, -2]
assert 36 == p6.x
assert -2 == p6.y
 
Point p8 = [36]
assert 36 == p8.x
assert 0 == p8.y</lang>
 
=====Map-to-property Substitution=====
There are several ways that a Map can be substituted for class properties. The process is properly construction and then property-mapping, which requires the existence of a no-argument constructor.
<lang groovy>// Direct map-based construction
def p3 = new Point([x: 36, y: -2])
assert 36 == p3.x
assert -2 == p3.y
 
// Explicit coercion from map with as keyword
def p5 = [x: 36, y: -2] as Point
assert 36 == p5.x
assert -2 == p5.y
 
// Implicit coercion from map (by type of variable)
Point p7 = [x: 36, y: -2]
assert 36 == p7.x
assert -2 == p7.y
 
// Implicit coercion from map (by type of variable)
Point p9 = [y:-2]
assert 0 == p9.x
assert -2 == p9.y</lang>
 
=={{header|Haskell}}==