Compound data type: Difference between revisions
Content deleted Content added
m copy edit - wiki link to wikipedia |
→{{header|Lua}}: add solution in Lua. |
||
Line 406: | Line 406: | ||
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> |
<lang logo>until [(first mousepos) < 0] [ifelse button? [pendown] [penup] setpos mousepos]</lang> |
||
=={{header|Lua}}== |
|||
==== Simple Table ==== |
|||
Lua could use a simple table to store a compound data type Point(x, y): |
|||
<lang lua> |
|||
a = {x = 1; y = 2} |
|||
b = {x = 3; y = 4} |
|||
c = { |
|||
x = a.x + b.x; |
|||
y = a.y + b.y |
|||
} |
|||
print(a.x, a.y) --> 1 2 |
|||
print(c.x, c.y) --> 4 6 |
|||
</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: |
|||
<lang lua> |
|||
cPoint = {} -- metatable (behaviour table) |
|||
function newPoint(x, y) -- constructor |
|||
local pointPrototype = {} -- prototype declaration |
|||
function pointPrototype:getX() return x end -- public method |
|||
function pointPrototype:getY() return y end -- public method |
|||
function pointPrototype:getXY() return x, y end -- public method |
|||
function pointPrototype:type() return "point" end -- public method |
|||
return setmetatable(pointPrototype, cPoint) -- set behaviour and return the pointPrototype |
|||
end--newPoint |
|||
</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: |
|||
<lang lua> |
|||
local oldtype = type; -- store original type function |
|||
function type(v) |
|||
local vType = oldtype(v) |
|||
if (vType=="table" and v.type) then |
|||
return v:type() -- bypass original type function if possible |
|||
else |
|||
return vType |
|||
end--if vType=="table" |
|||
end--type |
|||
</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: |
|||
<lang lua> |
|||
function cPoint.__add(op1, op2) -- add the x and y components |
|||
if type(op1)=="point" and type(op2)=="point" then |
|||
return newPoint( |
|||
op1:getX()+op2:getX(), |
|||
op1:getY()+op2:getY()) |
|||
end--if type(op1) |
|||
end--cPoint.__add |
|||
function cPoint.__sub(op1, op2) -- subtract the x and y components |
|||
if (type(op1)=="point" and type(op2)=="point") then |
|||
return newPoint( |
|||
op1:getX()-op2:getX(), |
|||
op1:getY()-op2:getY()) |
|||
end--if type(op1) |
|||
end--cPoint.__sub |
|||
</lang> |
|||
Usage example: |
|||
<lang lua> |
|||
a = newPoint(1, 2) |
|||
b = newPoint(3, 4) |
|||
c = a + b -- using __add behaviour |
|||
print(a:getXY()) --> 1 2 |
|||
print(type(a)) --> point |
|||
print(c:getXY()) --> 4 6 |
|||
print((a-b):getXY()) --> -2 -2 -- using __sub behaviour |
|||
</lang> |
|||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |