'Offline' Lua Prototyping

Hi Codeans,

Jut wanted to share what I have found to be a useful prototyping feature…

As my project progresses I find more and more that I need to prototype or port stuff like algorithms which can be a bit cumbersome to do in Codea…

If you install Lua onto your PC or Mac then you can add the folowing code snippet to the top of your Lua files which adds the same class() mechanism as Codea:

function class()
    return setmetatable({}, {
        __call = function(self, ...)
            self:init(...)
            return self
        end
    })
end

MyThing = class()

MyThing:init(x)
    self.x = x
end

```


This allows you to create and test offline Lua scripts that can be pasted directly into Codea...

I use IntelliJ IDEA (http://www.jetbrains.com/idea/free_java_ide.html) with a Lua plugin as a development environment which provides a nice coding editor with reformatting capability and features such as highlighting global instead of local variables etc.

Brookesi

If it is what I think it is, it sounds like a way to make Codea scripts on a PC or a Mac.
Did I misunderstand ? Please explain a little more.
Re Peter

@macflyerdk No, it isn’t. It’s a way to make Codea classes work in lua on a Mac or PC. The graphical stuff is additional. So if you have a non-graphical class that you want to test on your computer then you can do so. But if it uses the graphical stuff then you can’t (or, at least, it’s hard).

Actually, the full code for the class constructor is a bit more complicated than the above as it can deal with inheritance. It is (or at least, was. This might have changed since I last got a copy, but I doubt it):

--  
--  Copyright 2012 Two Lives Left Pty. Ltd.
--  
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--  
--  http://www.apache.org/licenses/LICENSE-2.0
--  
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
--  

-- Class.lua
-- Compatible with Lua 5.1 (not 5.0).

function class(base)
    local c = {}    -- a new class instance
    if type(base) == 'table' then
        -- our new class is a shallow copy of the base class!
        for i,v in pairs(base) do
            c[i] = v
        end
        c._base = base
    end

    -- the class will be the metatable for all its objects,
    -- and they will look up their methods in it.
    c.__index = c

    -- expose a constructor which can be called by <classname>(<args>)
    local mt = {}
    mt.__call = function(class_tbl, ...)
        local obj = {}
        setmetatable(obj,c)
        if class_tbl.init then
            class_tbl.init(obj,...)
        else 
            -- make sure that any stuff from the base class is initialized!
            if base and base.init then
                base.init(obj, ...)
            end
        end
        
        return obj
    end

    c.is_a = function(self, klass)
        local m = getmetatable(self)
        while m do 
            if m == klass then return true end
            m = m._base
        end
        return false
    end

    setmetatable(c, mt)
    return c
end

@macflyerdk It is a way of experimenting with portable non-graphical classes
@Andrew_Stacey, thank you