On 03/04/2011 06:26 PM, Michel Fortin wrote:
Another idea someone proposed (I can't find the exact post to give proper
credit) would be to allow having a parameter named 'this'. This would allow the
creation of non-member functions using the member syntax as with the Uniform
Function Call Syntax but without the "Uniform" part: only function specially
labeled this way would be eligible to the member call syntax. This would solve
our property problem, but is dependent on how UFCS is implemented.

Lua version (center on "add a new method"):

-- hand-baked Lua OO type
Point = {
    this = function (p, coords)
        point = {x=0, y=0}
        for k,v in pairs(coords) do
            point[k] = v
        end
        return setmetatable(point, Point)
    end ,
    __tostring = function (point)
        return "Point(" .. point.x .. "," .. point.y .. ")"
    end ,
}
Point.__call = Point.this
Point.__index = Point
setmetatable(Point, Point)

-- a point
point = Point {x=1, y=2}
print (point)           --> Point(1,2)

-- add a new method
Point.move = function (point, offset)
    point.x = point.x + offset.x
    point.y = point.y + offset.y
end

-- applicate added method (special syntax ':')
point:move {x=3, y=4}
print (point)           --> Point(4,6)


Denis
--
_________________
vita es estrany
spir.wikidot.com

Reply via email to