Comments (10)
The thing I like about symbols (other than the performance advantages) is that they somehow feel less like "magic constants". For example, in C people hate seeing things like:
int rate = 42;
They want to know "what the heck does 42 mean", so we tend to say:
#define STANDARD_FALL_RATE 42
int rate = STANDARD_FALL_RATE
I consider this to be a problem with strings as well. Let's say I have some control variable:
mode = "Normal"
That string bothers me because it implies that I can put anything there and there's some magic list that's going to check what I put in there against the list. You can make it more explicit by saying:
mode = :Normal
This implies that, although it is a string, it is a string that's SEMANTICALLY RELEVANT to the code-base.
from coffeescript.
hmm, why?
from coffeescript.
Ditto to jnicklas's question. What would this become? Compile symbols into globally-unique integers, or something like that?
from coffeescript.
javascript strings do not behave like ruby's strings; javascript has immutable strings where ruby does not. Ruby's symbols are immutable in part to meet the need for immutable string-like identifiers.
seriously though, try this in IRB:
irb(main):001:0> "foo".object_id == "foo".object_id
irb(main):002:0> :foo.object_id == :foo.object_id
Javascript doesn't have the same problems this implies with ruby strings.
from coffeescript.
Strings might be immutable in javascript, but comparing them is still more expensive than comparing integers. Here's a test (in Safari):
Still, the speed difference isn't so great that we need to add symbols to CoffeeScript.
from coffeescript.
Just wanted to add that I can certainly see not adding this to the language. I'm not sure that symbol support would pass your "unfancy" directive.
from coffeescript.
You make some very good points. Unfortunately, there's a critical wrinkle that pulls the rug out: You wouldn't be able to use symbols as keys in JavaScript objects (hashes). That's one of their primary uses in Ruby, and without it, there's not enough advantage to justify a new language concept. In addition, you wouldn't be able to meaningfully serialize them into JSON.
This is a good discussion to have on the record. Closing the issue...
from coffeescript.
Really a symbol is just an immutable string without spaces (in most cases), so you could just represent them as strings, but disallow allow certain operations on them. Whether it's worth it is another question...
from coffeescript.
I agree that symbols - if implemented - should simply be strings without certain operations. It's more about syntactic clarity and not as much about performance. In places where you would currently use a string from a preset number of possibilities, such as the eventName in a bind operation, readability would benefit from using a symbol instead of a general purpose string.
from coffeescript.
Symbols could be compiled to strings. Example:
:comments is 'comments'
:comments becomes 'comments' in compiled JS.
from coffeescript.
Related Issues (20)
- Unnecessary `splice` ref added for Array destructuring with rest element not in last position HOT 4
- Bug: Re-ordered nested non-end BindingRestElement doesn't get transpiled HOT 1
- CoffeeScript is fantastic, please donβt give it up HOT 1
- How to imitate `let` behavior in loops? HOT 5
- Proposal: Alternative file extension HOT 1
- Bug: Excessive variable and shallow copy for leading or middle rest parameter
- Proposal: Introduce `let` statement. HOT 6
- Proposal: Document Existential Operator Assignment
- Site issue: code blocks twitch on hover HOT 2
- Proposal: cake command should support ES6 modules HOT 2
- Need help understanding class member meanings HOT 1
- CLI `npm` `scripts` and input `.coffee` file/s as last argument conflicting with `--watch` HOT 2
- Bug: wrong code is transpiled for function call without parentheses HOT 2
- Proposal: Add end word to close method or class HOT 2
- Bug: Invalid indentation allowed after `do`
- Bug: Remove checkShebangLine multi arguments check HOT 1
- feature_request(html): backend CoffeeScript compilation inside HTML files HOT 3
- Bug: yield cannot be used in do -> expressions reliably? HOT 5
- [not an issue] An embeddable playground for CoffeeScript HOT 1
- Please help HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from coffeescript.