4 ms·
What's the symbol type for, and is Lua incapable of expressing it?
by anonymoushn 12y ago
What's the symbol type for, and is Lua incapable of expressing it?
- ufo 12y agoSymbol types[1] are useful for storing identifiers, method names or enums. They are common in LISPs and in Lua you use (immutable) strings for a similar purpose[2]. The main advantage of symbol types are that they have a more restricted API than strings and that they might be more efficient (integer vs string representation) [1] https://en.wikipedia.org/wiki/Symbol_%28Lisp%29 https://en.wikipedia.org/wiki/Symbol_%28Lisp%29 [2] For example, in Lua obj.foo is syntactic sugar for obj["foo"], similarly to Javascript.
- hisham_hm 12y agoSince you pointed out that Lua strings are immutable you're aware of what I'm about to say, but just to make it explicit: The efficiency argument is a common one for symbols in general, but in Lua it doesn't really hold because string comparison has constant cost (ie, it's a simple pointer test, since strings are immutable).
- to3m 12y agoImmutable and interned! (This may or may not be an issue, but it still offended me somewhat, since you're getting all of the compile-time cost at runtime, combined with none of the expressiveness. Not a tradeoff that impresses me, personally, even if the speed is fine - but it takes all sorts.)