Skip to content
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

add a 'length operator': #some_array #3637

Closed
blitmap opened this issue Sep 16, 2014 · 6 comments
Closed

add a 'length operator': #some_array #3637

blitmap opened this issue Sep 16, 2014 · 6 comments
Labels

Comments

@blitmap
Copy link

blitmap commented Sep 16, 2014

Hi. To put it short: I'm from Lua. In Lua we have the '#' operator to basically return the .length of an array. Arrays in Javascript are just objects with number keys -- in Lua this is the same, but the # length operator actually returns an internal/private C member tracking the array part of a Lua table.

ANYWAY: I would love to see this operator brought to Coffeescript. Yes, I am biased -- I'm used to using #. I love Coffeescript and this suggestion is purely cosmetic -- I don't often need .length because of Coffeescript's for loop syntax, but for other object types I'd love to see those .length's disappear.

(Please and thank you?)

this: #some?.thing?.here?
becomes: (some !== undefined && some.thing !== undefined && some.thing.here !== undefined && some.thing.here.length)

I'll look into making a patch but I am pretty new to Coffeescript. I love it so far <3

@vendethiel
Copy link
Collaborator

# wouldn't work anyway - it's a comment for us.

@blitmap
Copy link
Author

blitmap commented Sep 16, 2014

Yeah, forgot about that... better than // and /* */ for sure. :-)

I'm used to the valid characters for Lua identifiers, which is a much smaller set of characters than what JS (and Coffeescript) will allow.

I'd suggest :some_object as it looks almost like a range index, which might make people think of the length -- but it's not as clear as what Lua has with #some_object.

I dunno, I guess this suggestion can't work :3

@carlsmith
Copy link
Contributor

I don't personally see the value of adding an entire operator for just getting the length of arrays. If you're doing it a lot in some part of you code, just alias it.

len = (a) -> a.length
length = len someArray

@ysmood
Copy link
Contributor

ysmood commented Sep 17, 2014

It's sad to know that there are not enough operators for coffee to achieve it.

@carlsmith
Copy link
Contributor

It's nothing to do with a lack of operators. If you have an operator for length, why not one for forEach and indexOf etc? We'll end up with KlingonScript.

@blitmap
Copy link
Author

blitmap commented Sep 30, 2014

@carlsmith In Lua we just have # for quickly fetching the length of a table/hash/Object. We don't really lend ourselves to 'operator cancer'. len = (a) -> a.length works, but it's not as concise as just #1708

Oh well, # is more common as the comment prefix.... I don't hate it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants