Comments (19)
0 being false is pretty common, as boolean values in C are just integers.Empty string being false is odd tho.
from coffeescript.
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.
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.
I like that, but I'd say maybe use a keyword rather than an incomprehensible operator.
from coffeescript.
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.
defined? seems to just check undefined not null, so perhaps set?
from coffeescript.
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.
Couldn't you just write:
not set? x
?
from coffeescript.
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.
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.
That does make sense. Do you think it shouldn't check if it's defined ? Or could have a defined? operator
from coffeescript.
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.
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.
I really like it ... except I have a niggle: tooth_fairy is false --> tooth_fairy? is true
a little confusing?
from coffeescript.
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.
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.
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.
fair enough - sorry to batter you with half-thoughts :D
from coffeescript.
I approve of this, too, good work people.
from coffeescript.
Related Issues (20)
- Unnecessary `splice` ref added for Array destructuring with rest element not in last position HOT 4
- Bug: Re-ordered nested non-end BindingRestElement doesn't get transpiled HOT 1
- CoffeeScript is fantastic, please donβt give it up HOT 1
- How to imitate `let` behavior in loops? HOT 5
- Proposal: Alternative file extension HOT 1
- Bug: Excessive variable and shallow copy for leading or middle rest parameter
- Proposal: Introduce `let` statement. HOT 6
- Proposal: Document Existential Operator Assignment
- Site issue: code blocks twitch on hover HOT 2
- Proposal: cake command should support ES6 modules HOT 2
- Need help understanding class member meanings HOT 1
- CLI `npm` `scripts` and input `.coffee` file/s as last argument conflicting with `--watch` HOT 2
- Bug: wrong code is transpiled for function call without parentheses HOT 2
- Proposal: Add end word to close method or class HOT 2
- Bug: Invalid indentation allowed after `do`
- Bug: Remove checkShebangLine multi arguments check HOT 1
- feature_request(html): backend CoffeeScript compilation inside HTML files HOT 3
- Bug: yield cannot be used in do -> expressions reliably? HOT 5
- [not an issue] An embeddable playground for CoffeeScript HOT 1
- Please help HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from coffeescript.