lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


David Jeske wrote:
> > How does weak typing make code harder to understand?
>
> It makes it harder to understand because when someone's looking at an
> event callback in my game, it looks like:
>
> ge_collision = function(self,x,y,whoIhit)
> [...]

One thing to realize is that generic programming in C++ has the same issue,
and it's usually considered an advantage.  With generic programming you
don't care about the type of an object, only its interface.  Like others
have responded, a little documentation is helpful.  Here is David's function
again (simplified somewhat):

    -- whoIhit object must have get/setPos member functions
    function ge_collision(whoIhit)
        local xpos, ypos = whoIhit:getPos()
        whoIhit:setPos(xpos + 5, ypos);
    end

Here is a C++ version with the same meaning.  The whoIhit argument may be
any type, it just needs to have the proper member functions:

    // whoIhit object must have get/setPos member functions
    template <class T> void ge_collision(T& whoIhit)
    {
        float xpos, ypos;
        whoIhit.getPos(&xpos, &ypos);
        whoIhit.setPos(xpos + 5, ypos);
    }

Recently in the C++ community everyone is jumping on the generic programming
bandwagon.  What that language has taken years to evolve support for, Lua
has always been able to do... and with simpler syntax.

-John