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

1.1.3: existential @var? doesn't check for undefined #1869

Closed
lookfirst opened this issue Nov 16, 2011 · 12 comments
Closed

1.1.3: existential @var? doesn't check for undefined #1869

lookfirst opened this issue Nov 16, 2011 · 12 comments
Labels

Comments

@lookfirst
Copy link

This seems like a pretty minimal test case:

@a?

outputs:

this.a != null;

According to the documentation, "CoffeeScript's existential operator ? returns true unless a variable is null or undefined"

a?

correctly outputs:

typeof a !== "undefined" && a !== null;
@lookfirst
Copy link
Author

Most minimal test case does work:

@?

@michaelficarra
Copy link
Collaborator

See #1631

@michaelficarra
Copy link
Collaborator

Whoops, didn't realise it was you. Here's a more complete explanation: X == null tests that X is either null or undefined, assuming it is in scope. If we can't make that assumption, we need to do a typeof test to avoid ReferenceErrors. Please search for old issues before posting. There has to be a dozen that are exact duplicates.

@lookfirst
Copy link
Author

Apologies, I did search, but nothing came up that seemed relevant. There are so many closed issues now that searching through them is tiresome. Thanks for your patience.

It is disappointing that there doesn't seem to be a good solution for this issue. I was hoping to write some code that checked for null or undefined and there doesn't seem to be a good syntactic way to do that in CS.

@michaelficarra
Copy link
Collaborator

@lookfirst: X? does check for null or undefined.

$ node
> a = null
null
> a == null
true
> a = void 0
> a == null
true
> a = 0
0
> a == null
false
> 

@lookfirst
Copy link
Author

What I'd like to do is write this:

    methodFoo:
        @saveGroup(@d.groupName.val().trim(), @key?)

    saveGroup: (groupName = '', existing = false) =>
        if existing

What I ended up having to write is this:

    methodFoo:
        keyExists = typeof @key != "undefined" && @key != null
        @saveGroup(@d.groupName.val().trim(), keyExists)

    saveGroup: (groupName = '', existing = false) =>
        if existing

Maybe I'm just not understanding things correctly.

@michaelficarra
Copy link
Collaborator

@lookfirst: you're not understanding things correctly. @key? is fine, trust me. It produces true if the value of this.key is either undefined or null. Either one. Otherwise, it produces false. The equivalent javascript test that is being performed is this.key == null. Read up on the abstract equality comparison algorithm (section 11.9.3). Pay careful attention to steps 2 and 3.

@alpha123
Copy link

@lookfirst: Googling for site:github.com inurl:jashkenas/coffee-script/issues existential operator seems to find a lot of stuff.

@lookfirst
Copy link
Author

Ok, thanks for the pointers. I'm understanding things a lot more clearly now. Again, thanks for your patience.

@michaelficarra
Copy link
Collaborator

@alpha123: wow, I'm surprised how effective that is. Google's amazing. Thanks for the tip.

@jashkenas
Copy link
Owner

We should start a drinking game for every time this comes up ;)

@lookfirst
Copy link
Author

For all the beer I owe MF, we'd have to check him and his cute fuzzy bear into rehab after 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