lua-users home
lua-l archive

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


On 7/20/05, Ignacio Burgueño <ignacio@tecnolink.com.uy> wrote:
> Hi. I'd like to modify the parser so I could concatenate strings using '+'.
> I guess that I need to modify lparser.c, function subexpr. My initial
> attempt was to change the operand from OPR_ADD to OPR_CONCAT, but only if
> the operands are strings. This last part is what I couldn't figure out. Is
> this possible ?
>  
> Thanks in advance,
> Ignacio

No, it isn't possible. Consider this construction:

function foo(a,b)
    return a+b
end

Q: Are a and b strings or not? A: There's no way to know. This is
what's known as "static type analysis", and is only possible for
dynamically typed languages in special cases.

There's a good reason why + doesn't concatenate in Lua: to avoid
accidentally concatenating when you really meant to add. Since numbers
can be used in a string context and vice versa, this would generate
quite a few problems. Stick to .. for concatenation.

Ben