4 ms·
If you think that this language feature has no equivalent in your favorite language, it is similar to Ruby's :symbol, where it is mostly used as keys to their h
by espadrine 11y ago
If you think that this language feature has no equivalent in your favorite language, it is similar to Ruby's :symbol, where it is mostly used as keys to their hash tables (they're implemented with hashes internally).
Common Lisp has something even closer for its macro system. When you write code that writes code, you often need to create new variable names that won't collide or shadow anything. `(gensym)` does that: http://www.lispworks.com/documentation/lw50/CLHS/Body/f_gensym.htm#gensym http://www.lispworks.com/documentation/lw50/CLHS/Body/f_gens....
Here's a list of languages with support for symbols. You will see even Objective-C has it: https://en.wikipedia.org/wiki/Symbol_%28programming%29#Support https://en.wikipedia.org/wiki/Symbol_%28programming%29#Suppo....
I'd be curious to learn how it is implemented in JS engines.
- inglor 11y agoLike a property basically - except the key is not a string and is known to the engine. This is both simple and uses existing JIT facilities. Symbol property access isn't particularly slower than regular access this way too. See http://blog.peschla.net/doxygen/v8_chromium_r157275/classv8_1_1internal_1_1_symbol_key.html http://blog.peschla.net/doxygen/v8_chromium_r157275/classv8_... - it's
- masklinn 11y ago> Here's a list of languages with support for symbols. You will see even Objective-C has it: https://en.wikipedia.org/wiki/Symbol_%28programming%29#Suppo... https://en.wikipedia.org/wiki/Symbol_%28programming%29#Suppo.... None of the symbols in that table match the purpose and use case for `new Symbol`, though gensym does.