Giter Club home page Giter Club logo

Comments (19)

liamoc avatar liamoc commented on July 23, 2024

0 being false is pretty common, as boolean values in C are just integers.Empty string being false is odd tho.

from coffeescript.

weepy avatar weepy commented on July 23, 2024

Yes, but C doesn't have a null for integers. I was thinking that an operator that checked for null, undefined and false would be useful. E.g.

if(^s and ^^x): do_stuff()

if( (s===null || s=== undefined || s === false) && !(x===null || x=== undefined || x === false) ) { do_stuff() }

from coffeescript.

weepy avatar weepy commented on July 23, 2024

or even
if( (typeof s != "undefined" && (s===null || s=== undefined || s === false)) && (typeof x == "undefined" || (x===null || x=== undefined || x === false) ) { do_stuff() }

Just an idea

from coffeescript.

liamoc avatar liamoc commented on July 23, 2024

I like that, but I'd say maybe use a keyword rather than an incomprehensible operator.

from coffeescript.

weepy avatar weepy commented on July 23, 2024

could use something like if set? x or if defined? x

I quite like the use of the ruby style ? - as it means it won't collide with JS vars

from coffeescript.

liamoc avatar liamoc commented on July 23, 2024

defined? seems to just check undefined not null, so perhaps set?

from coffeescript.

jashkenas avatar jashkenas commented on July 23, 2024

Ok, so here's the list of falsy things in JavaScript:

  • false
  • 0
  • 0.0
  • null
  • ""
  • undefined
  • NaN

You're suggesting an keyword that returns false for null and undefined only, right? The rest would return true. This seems similar to !obj.nil? in Ruby. If we provide it, we should also provide a keyword for the inverse.

from coffeescript.

liamoc avatar liamoc commented on July 23, 2024

Couldn't you just write:

not set? x

?

from coffeescript.

weepy avatar weepy commented on July 23, 2024

I was trying to mirror ruby, which feels the most natural in my experience.

i.e. set? x would mean not null, undefined or false.

There's obviously several different possible permutations here. I suppose the best would be to cover the most common use case.

from coffeescript.

jashkenas avatar jashkenas commented on July 23, 2024

That's the trick, isn't it -- I think that set? (or whatever we call it) should definitely not include false alongside null and undefined. If you set a boolean variable to false, you've still set it. Null should be used to indicate that a variable has no value.

In Ruby, false.nil? is false.

from coffeescript.

weepy avatar weepy commented on July 23, 2024

That does make sense. Do you think it shouldn't check if it's defined ? Or could have a defined? operator

from coffeescript.

jashkenas avatar jashkenas commented on July 23, 2024

Here's an idea: Since CoffeeScript only has implicit ternary operators, and javascript doesn't support question marks as part of identifiers, we could use the ? symbol, postfix, to mean "existence".

earn(0.25) if tooth_fairy?

Could compile into something like:

if (tooth_fairy != undefined) {
  earn(0.25);
}      

Using the == and != operators against undefined, because they also return true for null, and null only, due to the coercion. See:

http://stackoverflow.com/questions/27509/detecting-an-undefined-object-property-in-javascript

Sound ok?

from coffeescript.

jashkenas avatar jashkenas commented on July 23, 2024

Alright. It's now on master, and doesn't use the above technique, which doesn't work when the variable is really undefined (Bad StackOverflow!). Instead, it compiles:

earn(0.25) if tooth_fairy?

Into:

if ((typeof tooth_fairy !== 'undefined' && tooth_fairy !== null)) {
  earn(0.25);
}

Closing the ticket...

from coffeescript.

weepy avatar weepy commented on July 23, 2024

I really like it ... except I have a niggle: tooth_fairy is false --> tooth_fairy? is true

a little confusing?

from coffeescript.

jashkenas avatar jashkenas commented on July 23, 2024

A little, but I think it's a matter of perspective. If we introduce it as meaning "exists", from the very beginning, then it hopefully won't be too confusing.

Note that it's still similar to tooth_fairy.nil? in Ruby. If you've set it to false, then it's going to return true. If you really want to check for false, then use if var isnt false, or just unless var.

What we're trying to go for here is the case that's difficult to detect for in javascript -- if you're trying to see if a string has been defined, you have to watch out for "", if a number, watch out for 0 and NaN, if a boolean, false...

from coffeescript.

weepy avatar weepy commented on July 23, 2024

you make a fair case on a difficult point.

do you think there's any case for other keyword/operators that work like questions ?

If we go with chunky_bacon? then we can't use any others, where as if we used set? chunky_bacon, then we can have defined? chunky_bacon (for example) and others.

from coffeescript.

jashkenas avatar jashkenas commented on July 23, 2024

I'm hoping that we won't need to introduce other keywords without a real need. That said, if we need to, we can still have them -- the lexer would pick out defined? as a keyword before it ever got around to the ? operator.

Do you have a real-world use case of needing a defined? obj where obj? wouldn't work?

from coffeescript.

weepy avatar weepy commented on July 23, 2024

fair enough - sorry to batter you with half-thoughts :D

from coffeescript.

liamoc avatar liamoc commented on July 23, 2024

I approve of this, too, good work people.

from coffeescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.