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

suggestion: Null-Coalescing Operator #128

Open
gavinengel opened this issue Feb 5, 2015 · 4 comments
Open

suggestion: Null-Coalescing Operator #128

gavinengel opened this issue Feb 5, 2015 · 4 comments

Comments

@gavinengel
Copy link

This syntax seems to be a more compact ternary operator, is that right?

// spider syntax:
var name = somethingElse;
name = name ?? 'default name';

If so, may I suggest a slightly different form? Per a suggestion here, I re-write the above which checks a variable and defaults it if the variable is empty/false:

// suggested formats, all of which are the same
var name = somethingElse;
name = name ? name : 'default name'; // traditional ternary
name = name ?: 'default name'; // shortened ternary <-- this looks better to me
name = name || 'default name'; // just like the one above
name = name ?? 'default name'; // spider syntax
name ?= 'default name'; // really short ternary  <-- I also like this
@fnky
Copy link

fnky commented Feb 6, 2015

Spider got its tenary operator style from Swift. I also thought of doing the last style. It's much more compact and still easy to distinguish.

@Namek
Copy link
Collaborator

Namek commented Feb 6, 2015

This was discussed already in #97, some other topics and AFAIR on IRC.

Personally, I see no reason to change ??. Take a look at @alongubkin's response in #97 to understand why.

Just a note from me about name ?= 'default name'; this one is just confusing when you don't know the language. And what if we do name ?= someVariableWithUndefinedValue when name is already defined? Confusing IMO.

@fnky
Copy link

fnky commented Feb 6, 2015

I see the point of not changing ??. However, foo ?= baz could as well be a shortcut to foo = foo ?? baz for a shorter syntax:

function add(a, b) {
  b ?= a; // b = b ?? a
}

and you can still use ?? to allow something like this

function go(opts) {
  var cake = opts.cake ?? 'lie';
}

@gavinengel
Copy link
Author

Good points, all. I only wanted to give some food for thought. I see this topic has been discussed before. Thanks for reading my issue.

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

No branches or pull requests

3 participants