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

Allow if(!exists someVariable) #817

Closed
lucaswerkmeister opened this issue Oct 13, 2013 · 8 comments
Closed

Allow if(!exists someVariable) #817

lucaswerkmeister opened this issue Oct 13, 2013 · 8 comments
Assignees
Milestone

Comments

@lucaswerkmeister
Copy link
Member

The following code is illegal:

variable Type? cached = cache[query];
if (!exists cached) {
    // calculate
    cache[query] = calculated;
    cached = calculated;
}
// use

Instead, you have to write

if (!is Object cached)

or

if (is Null cached)

I don't see any point in this, since exists is just a shortcut for is Object anyways.

@lucaswerkmeister
Copy link
Member Author

See section 5.4.5 of The Ceylon Language Specification.

Should this also apply to if (!nonempy something)? Seems a bit redundant to have two negations in there...

@lucaswerkmeister
Copy link
Member Author

In the grammar, isCondition begins with an optional NOT_OP, while existsCondition doesn't. In the nodes list, IS_CONDITION has a boolean not, while EXISTS_CONDITION doesn't.

@lucaswerkmeister
Copy link
Member Author

Well, my example is a bit weak - I ended up using if (exists cached = cache[query]), which is probably nicer. I still think that if (!exists should be allowed, though.

@gavinking
Copy link
Member

The only reason we don't support this is that it's a bit useless, at least until we get #74. I mean, there's just no value in narrowing the type of something to Empty or Null. OTOH, when we have #74, this will obviously be useful and we will need it then.

@lucaswerkmeister
Copy link
Member Author

Oh, I didn't see #74 and #170. You're totally right, it makes a lot more sense with that.

Should we close this issue then? if (!exists was proposed in the other issues as well, so this one's a bit of a duplicate.

@gavinking
Copy link
Member

Nono, leave it open, it's a distinct issue.

@sgalles
Copy link

sgalles commented Jan 7, 2014

This feature would also be useful for #891

@lucaswerkmeister
Copy link
Member Author

Done in #74, particularly d024d99.

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

3 participants