lua-users home
lua-l archive

Re: The meaning of 'sugar' [Forked from Re: Why do we have ipairs?]

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


On 06/13/2014 09:07 AM, Ross Bencina wrote:
On 13/06/2014 4:13 PM, Thomas Jericke wrote:
I'll leave it to others to determine whether using string literals in
an interface is ever good style.
Ross.
Show me a Lua API that doesn't use string literals.
local myLib = require "MyLib" -- Oops a string literal
myLib.myFunction()
-- which is syntactic sugar for:
myLib["myFunction"]() -- Oops, another one.
So without sting literals, you cannot use globals, require, access table
elements of type string.
You're intentionally twisting my words.
No that is not my intention. I honestly that Lua builds heavily on string literals.
It's one thing to be able to use a string literal, quite enough to require its use in an interface.
But Lua does require to use strings literals almost everyone. In my example I even forgot one string literal:
require "MyLib" -- is syntactic sugar for
_ENV["require"]("MyLib")
We're not talking about module or function names here, we're talking about constants that are usually integers.
Ross.
I don't know how you store integer "constants" in Lua but I store them in a global or a table element.
SomeContants = {
 constant1 = 42,
 constant2 = 1773
}
And then I use it:
Any.Function("hello", SomeConstants.constant1)
And the same could be used in a case statement:
switch SomeInteger
 case SomeConstants.constant1
 print "SomeInteger was 41"
 case SomeConstants.constant2
end
But of course there is a catch. The switch case table would have to be built up every time because, SomeConstants is not really constant. So I agree, this is not suitable. So I see your problem with the problem if you want to switch for a number, but I don't see a problem with string literals in a Lua interface. Actually as long as I stay in the Lua worlds I prefer string literals over numbers as long as the number is only used as an enumeration.
--
Thomas

AltStyle によって変換されたページ (->オリジナル) /