-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Terser array syntax #2259
Comments
I have read through all three topics and I find rpflorence's solution beautiful:
I can't seem to find any ambiguity. Also, seems to me this is backwards compatible. |
I'm warming up to it ever so slightly. It seems like we would want this too?:
You wouldn't actually need the {} in most circumstances, but it allows you to be explicit about creating a hash, and it makes it easy to transition from a zero-key hash to a one-key hash. I'm +0.0000000000001 on this. |
I could dig: foo = []
'bar'
'baz'
foo = {}
bar: 'baz' Both of which are parse errors right now (good news). Or to be more clear, make the foo = []>
'bar'
'baz'
foo = {}>
bar: baz |
seems unnecessary. Objects are already easily distinguishable by using key value pairs and there is no "third" type to objects and arrays (in which case it would make sense to unify all of them). Going from zero to one element hash does look great but it also adds two characters to type for each non-empty object ([] doesnt, since now you need brackets anyway). But I think there would be great use for it in anonymous objects' declaration:
It could also clarify this, which seems to me rather non-obvious (I don't know how popular it is):
|
Let's not add more ambiguous stuff. It took me a long time to get used to the return of implicit objects (like below). because it has two of the 'magics' of coffee-script: auto-return and brace-less objects:
If you add bracket-less arrays, make sure they aren't confusing. |
Here, the array's items are between the square brackets: array = ['foo', 'bar', 'baz'] The same is true in this case: array = [
'foo'
'bar'
'baz'
] But not in this case: array = []
'foo'
'bar'
'baz' I believe an array's items belong between the delimiters ( |
Here the function bodies are between the parens: foo.then (-> doStuff()), (-> doOtherStuff()) But not in this case: foo.then ->
doStuff()
, ->
doOtherStuff() Here, the object's items are between the curly brackets: obj = { foo: 'bar', baz: 'qux' }
obj = {
foo: 'bar'
baz: 'qux'
} But here they are not: obj = foo: 'bar', baz: 'qux'
obj =
foo: 'bar'
baz: 'qux' I believe a function body, objects' items, and arrays' items belong wherever is the cleanest :) Perhaps my proposed syntax isn't clean (that's subjective), but this kind of sugar isn't unprecedented in coffeescript. |
Dropping punctuation is not the same as using punctuation in a new way. I love being able to write array = 'foo', 'bar', 'baz'
array =
'foo'
'bar'
'baz' I'm not suggesting that this is even a possibility; my point is that the proposed syntax is not a natural extension of CoffeeScript's bracket-free object literal notation. |
@davidchambers these are nice, but what happens in a function call? And in a magic return ? a = ->
"a"
"b" |
Coco enables this by making
This is doable being currently invalid:
unlike the inline version:
|
As rpflorence pointed out, if objects can lose brackets, why arrays couldn't? Making completely bracket-free arrays has several issues though as was pointed out here and in the other threads. // new optional way to declare arrays
array = []
el
// new optional way to declare objects
object = {}
bla: do
// real uses
object =
key: value
array = []
da
ba
array = []
{}
key: value
{}
key: value
[]
doom
// making implicit objects more obvious
f = ->
{}
key: value All backwards compatible (doesn't compile now) and not ambiguous in my opinion. |
Yaml-like syntax is also not bad |
So, let's walk through this a bit... First -- Coco's change is the most appealing of these, because it simply allows lists of things as individual lines. All appears well in the obvious case:
... but if you allow that, you also have to allow the single-element array:
... which is deeply unintuitive, but still perhaps acceptable. Where you really get into trouble is with function calls:
... The serious ambiguity of those two is why we aren't going to do it. As to the proposal to prefix lists of items with |
To be honest.. I really want some feature because if we can do that, dowload-link = "http://jiyinyiyong.github.com/live-tmpl/page/tmpl.js"
page-link = "http://jiyinyiyong.github.com/live-tmpl/page/"
repo-link = "https://github.com/jiyinyiyong/live-tmpl/"
hope-words = '''
JSON is a build-in type in JavaScript. Hope everyone enjoys it.
More often, we need to deal with HTML in browsers.
For Example, the client templating in single-page apps.
So, why is it ugly and awful to generate HTML with JS?
Now I'm writting live-tmpl, trying to make it easy in LiveScript.
'''
hope-html = hope-words.split "\n" .map -> p: it.trim!
demo =
"/root.main name='top'":
* "h3": "Home page of Live-tmpl"
* ".list"
* ".line":
* "span.title": "Goal"
* ".list": hope-html
* ".line":
* span: "by"
* span: "Jiyin Yiyong"
* ".line":
* span: "in"
* span: "LiveScript"
* ".links":
* p: "Some links here:"
* ".line":
* "a href='#dowload-link'": "Download"
* "a href='#page-link'": "Demo Page"
* "a href='#repo-link'": "Github Repo" |
I couldn't find an issue that already talks about this, so please consider this code:
Wonder if we could do something like:
Or anything else that makes sense and doesn't require a closing character.
The text was updated successfully, but these errors were encountered: