Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to overload operators for strings?

Is it possible to add your own operators and metamethods to strings in Lua?

I wish to do something like this:

local str = "test"

print(str[2]) --> "e"
print(str()) --> "TEST"
print(-str) --> "tset"
print(str + "er") --> "tester"
print(str * 2) --> "testtest"
like image 414
Mossarelli Avatar asked Dec 21 '16 11:12

Mossarelli


People also ask

Which operator is overloaded for strings?

+ and += are overloaded for String objects. There are many other examples of operator overloading in Java. For instance & , | , and ^ are overload for boolean and integral types. And indeed, the arithmetical and relational operators are overloaded for various numeric types.

Which operators Cannot be overload?

Master C and Embedded C Programming- Learn as you go These operators cannot be overloaded because if we overload them it will make serious programming issues. For an example the sizeof operator returns the size of the object or datatype as an operand. This is evaluated by the compiler.

How do you overload a string in C++?

To concatenate two strings using unary operator overloading. Declare a class with two string variables. Create an instance of the class and call the Parameterized constructor of the class to initialize those two string variables with the input strings from the main function.

Can I overload operator == so it lets me compare two char [] using a string comparison?

[13.6] Can I overload operator== so it lets me compare two char[] using a string comparison? No: at least one operand of any overloaded operator must be of some class type.


1 Answers

Yes, using a "secret" metatable for strings in Lua, it is possible to define a number of different operator overloads for strings.

Try the following code:

getmetatable('').__index = function(str,i) return string.sub(str,i,i) end
getmetatable('').__call = function(str,i) return string.upper(str) end
getmetatable('').__unm = function(str,i) return string.reverse(str) end
getmetatable('').__add = function(str,i) return (str .. i) end
getmetatable('').__mul = function(str,i) return string.rep(str, i) end

local str = "test"

print(str[2]) --> "e"
print(str()) --> "TEST"
print(-str) --> "tset"
print(str + "er") --> "tester"
print(str * 2) --> "testtest"

The reason you cannot use setmetatable('',...) is because it can only be used on tables. But with the "hack" above, you can easily insert different methods into strings.

Remember to only use this locally in your own code if you really need it, as there might be conflicts with using this globally in projects.

The proper way to manipulate strings or data however, is to use methods from a module. The metamethods provided in this example is not OOP friendly and affects all strings in the scope in Lua after it has been defined. Lua gives us the power to edit string's metatable, but we should use it with responsibility.

There's nothing that str:sub(), str:upper() and the other methods can't do with examples like these which should be used before changing the meta functionality of strings in the whole program.

like image 84
Mossarelli Avatar answered Oct 04 '22 20:10

Mossarelli