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

Docs: enhance documentation of existential operators #1631

Closed
michaelficarra opened this issue Aug 27, 2011 · 15 comments
Closed

Docs: enhance documentation of existential operators #1631

michaelficarra opened this issue Aug 27, 2011 · 15 comments
Assignees

Comments

@michaelficarra
Copy link
Collaborator

Right now, the documentation for our existential operators is either poor or non-existent. We should list all of the following operators along with proper definitions of their behaviour and possibly individual names (taken from #1613, the issue that inspired this one):

  • unary ?:
    • a? tests that a is in scope and a != null
  • binary ?:
    • a ? b returns a if a is in scope and a != null; otherwise, b
  • ?. and ?[ ]: soaked property access:
    • a?.b or a?['b'] returns a.b if a is in scope and a != null; otherwise, undefined
  • ?( ): soaked function invocation:
    • a?(b, c) (or a? b, c) returns the result of calling a (with arguments b and c) if a is in scope and callable; otherwise, undefined
  • ?=: existential assignment:
    • a ?= b assigns the value of b to a if a is not in scope or if a == null; produces the new value of a

Some particular parts of the current documentation that bother me:

  • "fails for zero, the empty string, and false"
    • the list should be exhaustive or somehow acknowledge that it is not
  • "returns true unless a variable is null or undefined"
    • doesn't mention how undeclared variables are handled
  • doesn't really distinguish between the different operators
@jashkenas
Copy link
Owner

++, this would be great. We just need to be sure that the section doesn't get too long on the page, and still feels bite-sized.

Direct documentation patches can be committed to master, and cherry-picked over to gh-pages.

@CRogers
Copy link

CRogers commented Sep 27, 2011

Just from the viewpoint of a user, I recently tried to start using the existential operator but the docs didn't seem to correspond with what was actually happening and I ended up not using it, so +1 to this.

@tylermumford
Copy link

That's true. The documentation states that the unary ? operator checks to see if its argument is not undefined and not null, and the example compilation corresponds to that behavior: (typeof mind !== "undefined" && mind !== null). Compiling on the fly using the "Try CoffeeScript" input (which is brilliant, by the way) on the website also compiles to this code.

However, in my compilations, it leaves out undefined-checking, and simply becomes mind != null (which even has a different relational operator). Was this change made in a previous release and then missed in the documentation updates?

I'm using the Node.js executable method on Windows 7 64-bit, and coffee --version reads as "1.1.3-pre".

@michaelficarra
Copy link
Collaborator Author

Here we go again. This needs to be mentioned somewhere in the docs as well to avoid getting this question all the time.

@TylerWayne: x == null is a test for either an undefined value or null, but it's only safe to use when we know the identifier is in scope. When we can't be sure that it's in scope, we have to do the typeof test to avoid a ReferenceError.

@jashkenas
Copy link
Owner

@michaelficarra: Your patience is infinite.

I don't think that mentioning this in the docs is going to stop anyone from asking the question -- people just look at the generated output and open a ticket or comment immediately.

@tylermumford
Copy link

My apologies; thank you very much for clarifying. Yes, it would be very good to get this in the docs for novice developers like myself.

@michaelficarra
Copy link
Collaborator Author

@jashkenas: I think it may actually help. @TylerWayne said that the main source of his confusion was not the compilation itself, but the disparity between what he saw and what was listed in the documentation. It probably won't help with cases where someone sees two different compilations in their own code, not realising that one usage is on an undeclared variable, but it may help with instances like @TylerWayne's. Maybe. At the very least, it will educate people about a good javascript practice.

@showell
Copy link

showell commented Oct 21, 2011

+1 on better documentation, even if nobody reads it. If nothing else, when the questions come up in the future, you can point folks at the docs.

@ghost ghost assigned jashkenas Oct 21, 2011
@franc
Copy link

franc commented Oct 24, 2011

I understand @jaskenas' reasons for wanting to keep documentation byte-sized, clear and minimalist - it speaks to what CS is all about, and is a quick and clear intro.

On the other hand it would be fantastic to have some more detailed documentation, and examples of common use etc. Perhaps the official documentation could link to a wiki page on each sub section? where we can flesh it out as a real reference rather than just a quick and powerful intro.

@wmayner
Copy link

wmayner commented Oct 10, 2013

Big +1 for this, particularly regarding the part that says

CoffeeScript's existential operator ? returns true unless a variable is null or undefined, which makes it analogous to Ruby's nil?

…which is very confusing to the less experienced among us. Maybe just a couple of sentences right after that paragraph? Something like:

Note: the ? operator only checks for null or undefined if the variable in question isn't declared. If it is declared, then it only checks if the variable is null, since the variable is already known to be in scope.

And then maybe some code blocks showing that

declared = 0
declared?
undeclared?

compiles to

var declared;
declared = 0;
declared != null;
typeof undeclared !== "undefined" && undeclared !== null;

Incidentally, I'm in favor of a complete, authoritative reference to go along with the intro docs—but I think this particular declared/undeclared thing is confusing enough that it warrants at least some mention even in the concise version.

@ymeine
Copy link

ymeine commented Oct 10, 2013

But declared != null; also checks if it is undefined: http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3 (points 1.a and 1.b correspond to strict comparison, and 2 and 3 to the behavior I'm mentioning)

@vendethiel
Copy link
Collaborator

It checks if its value is undefined, which will result in a TypeError if the variable isn't declared.

@ymeine
Copy link

ymeine commented Oct 10, 2013

@Nami-Doc yes, I was just telling that the documentation addition suggested by @wmayner was not correct in fact

However I noticed this exact behavior (the TypeError) while doing some tests, and now I understand why CoffeeScript compiles to typeof undeclared !== "undefined" && undeclared !== null; in case it doesn't now if the reference is actually accessible or not. Thanks! :)

(so if something should be added to the documentation, I think that would be that.)

@wmayner
Copy link

wmayner commented Oct 10, 2013

@ymeine Thanks for that correction; I didn't realize that. In that case, I'm with you—some mention of that behavior should be in the documentation, perhaps even just a footnote.

@GeoffreyBooth GeoffreyBooth changed the title enhance documentation of existential operators Docs: enhance documentation of existential operators Apr 29, 2017
GeoffreyBooth added a commit to GeoffreyBooth/coffeescript that referenced this issue May 2, 2017
@GeoffreyBooth GeoffreyBooth mentioned this issue May 2, 2017
13 tasks
GeoffreyBooth added a commit that referenced this issue May 6, 2017
* Docs: named functions and function declarations

* No more prototypal `extends`; update docs and example

* More comprehensive documentation of the existential operator; closes #1631

* Better document operators, including `from`

* No fat arrow class methods anymore

* Destructuring shouldn’t say that default values are applied in case of undefined or null

* Spinoff generator and async functions into their own sections; reorder things so that the sections on functions come just before classes, and destructuring goes next to the operators (which discuss assignment)

* Rewrite “CoffeeScript 2” section, making it less practical and more explanatory; move practical info into “Usage”

* Update “Variable Scoping and Lexical Safety” section to remove incorrect reference to Ruby (fixes #2360), add missing details about the safety wrapper, add note about `let`/`const`.

* Updated browser compiler

* Updated docs

* Rewrite Literate CoffeeScript breaking changes

* Split apart the “Breaking Changes” and “Unsupported Features” sections into separate sidebar items and files

* Add example of `not in`, closes #3281

* Fix words in bold that should be in backticks

* Consolidate some breaking changes sections

* Add Node API documentation; closes #3551

* Move the chaining documentation out of the changelog into its own section
@GeoffreyBooth
Copy link
Collaborator

Done in #4536.

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

No branches or pull requests

10 participants