CLJS: undefined and nil are "equally" nil?

1,008 views
Skip to first unread message

Frank Siebenlist

unread,
Jan 15, 2013, 12:17:09 AM1/15/13
to Clojure, Frank Siebenlist
ClojureScript:cljs.user> (def a nil)
nil
ClojureScript:cljs.user> (def b)

ClojureScript:cljs.user> (undefined? a)
false
ClojureScript:cljs.user> (undefined? b)
true
ClojureScript:cljs.user> (nil? a)
true
ClojureScript:cljs.user> (nil? b)
true
ClojureScript:cljs.user> (type a)
nil
ClojureScript:cljs.user> (type b)
nil
ClojureScript:cljs.user> (goog.typeOf a)
"null"
ClojureScript:cljs.user> (goog.typeOf b)
"undefined"
ClojureScript:cljs.user> (= a b)
true


That doesn't feel right… does it?

Definitely different from clojure… although it's difficult to compare without vars in cljs.

-FrankS.



David Nolen

unread,
Jan 15, 2013, 12:25:46 AM1/15/13
to clo...@googlegroups.com
This behavior is desirable. Unless you are in some dark cave of interop you shouldn't care.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Frank Siebenlist

unread,
Jan 15, 2013, 1:08:27 AM1/15/13
to clo...@googlegroups.com, Frank Siebenlist
ClojureScript:cljs.user> (def a nil)
nil
ClojureScript:cljs.user> (def b)

ClojureScript:cljs.user> (= a b)
true
--------
user=> (def a nil)
#'user/a
user=> (def b)
#'user/b
user=> (= a b)
false
--------

I didn't realize that the above is dark cave material ;-)

Could you elaborate a little on the desirable aspect?
(I'm not arguing - only trying to understand as I have just been bitten by this behavior)

Thanks, FrankS.

Jordan Berg

unread,
Jan 15, 2013, 2:47:35 AM1/15/13
to clo...@googlegroups.com
I find this to be useful:

cljs.user> (nil? (.-key (js-obj)))
true

I like not having to use undefined? for this

David Nolen

unread,
Jan 15, 2013, 8:32:32 AM1/15/13
to clojure
This is an unrelated issue. We could probably simulate this by creating a Unbound type and initializing def'ed vars without init expressions to instances of it.

David

Frank Siebenlist

unread,
Jan 15, 2013, 12:33:35 PM1/15/13
to clo...@googlegroups.com, Frank Siebenlist
I opened issue CLJS-457 for this enhancement/bug.

As a workaround, one can test for undefined?, although that is not really the same and cljs-specific, but for my use case that will do.

Thanks, Frank.

David Nolen

unread,
Jan 15, 2013, 1:30:04 PM1/15/13
to clojure
Thanks, will look into it.
Reply all
Reply to author
Forward
0 new messages