Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

What's the symbol type for, and is Lua incapable of expressing it?



Symbol 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

[2] For example, in Lua obj.foo is syntactic sugar for obj["foo"], similarly to Javascript.


Since 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).


Immutable 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.)




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: