Clojurescript - Javascript constructor and namespace with the same name problem

495 views
Skip to first unread message

Marko Kocić

unread,
Jul 28, 2011, 10:41:12 AM7/28/11
to clo...@googlegroups.com
Hi all,

When dealing with ClojureScript and Closure library it happens pretty often that Closure namespace is in the same time constructor for some object.

Take a look for this example:

(ns notepad
  (:require 
     [goog.dom :as dom]
     [goog.ui.Zippy :as Zippy]))

First, require forces me to require goog.ui.Zippy as Zippy and later in the code I have to use fully qualified name instead of provided one.

This works
(goog.ui.Zippy. headerElement contentElement)

This doesn't work, since Zippy is namespace declaration
(Zippy. headerElement contentElement)

I know that we can't have both namespace and function with the same name, but this is pretty frequent situation in Closure library, and is a bit awkward.
One solution would be that namespace :as symbol is specialcased so that without namespace prefix Zippy and Zippy. works like a regular function, and when in place of namespace prefix, it works as a namespace prefix. That would be pretty in line with Closure library itselfi.

Then we would be able to use
(require [goog.ui.Zippy :as Zippy])
(def z (Zippy. "ttt" "sss")) ;; same as calls goog.ui.Zippy.
(Zippy/someMethod x) ;; same as goog.ui.Zippy

What would be your proposal for this?

Chris Granger

unread,
Jul 31, 2011, 12:57:37 AM7/31/11
to Clojure
FWIW, one work around for this is to include the sub-namespace as well
and reference it from that one. So in your example:

(ns notepad
(:require
[goog.dom :as dom]
[goog.ui :as ui]
[goog.ui.Zippy :as Zippy]))

(ui/Zippy. "ttt" "sss")

David Nolen

unread,
Jun 11, 2012, 12:49:05 PM6/11/12
to clo...@googlegroups.com
This is an known existing issue. Using :require to important constructors just doesn't make sense.

2 patches I think could help here:

1) support for :import
2) support for :require w/o :as

Anyone game to submit some fixes?

David

--
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

tomoj

unread,
Jun 11, 2012, 1:37:14 PM6/11/12
to clo...@googlegroups.com
I noticed that it works fine to just do (:require [goog.async.Deferred :as d]) and to use (goog.async.Deferred.) to call the constructor (in this case Deferred is also used as a namespace, so require makes sense).

With :require w/o :as support, you'd have (:require goog.async.Deferred) ... (goog.async.Deferred.), right?

So with :import, (:import goog.async.Deferred) ... (Deferred.)? I would have expected (:import goog.async.Deferred.CancelledError) to cause trouble here, but in this case at least, deferred.js also provides goog.async.Deferred.CancelledError.
David

David Nolen

unread,
Jun 11, 2012, 1:41:42 PM6/11/12
to clo...@googlegroups.com
On Mon, Jun 11, 2012 at 1:37 PM, tomoj <t...@tomoj.la> wrote:
I noticed that it works fine to just do (:require [goog.async.Deferred :as d]) and to use (goog.async.Deferred.) to call the constructor (in this case Deferred is also used as a namespace, so require makes sense).

With :require w/o :as support, you'd have (:require goog.async.Deferred) ... (goog.async.Deferred.), right?

Yes.
 
So with :import, (:import goog.async.Deferred) ... (Deferred.)? I would have expected (:import goog.async.Deferred.CancelledError) to cause trouble here, but in this case at least, deferred.js also provides goog.async.Deferred.CancelledError.

I think the following should work:

(:import goog.async.Deferred)

(:import [goog.async Deferred ...])

Other thoughts?

David

Michał Marczyk

unread,
Jun 11, 2012, 9:16:16 PM6/11/12
to clo...@googlegroups.com
On 11 June 2012 18:49, David Nolen <dnolen...@gmail.com> wrote:
> Anyone game to submit some fixes?

I am. I'll create a new ticket for :import and build :require w/o :as
on top of that (attaching patch to CLJS-272).

M.

Michał Marczyk

unread,
Jun 11, 2012, 9:22:22 PM6/11/12
to clo...@googlegroups.com
See

http://dev.clojure.org/jira/browse/CLJS-312

for :import (patch attached). Looking at 272 now...

David Nolen

unread,
Jun 11, 2012, 9:22:49 PM6/11/12
to clo...@googlegroups.com
Thanks!

Michał Marczyk

unread,
Jun 11, 2012, 9:36:40 PM6/11/12
to clo...@googlegroups.com
Patch attached to 272 (note it's created on top of 312).

Cheers,
M.

David Nolen

unread,
Jun 14, 2012, 10:27:08 AM6/14/12
to clo...@googlegroups.com
:require now no longer requires :as. :require now also supports :refer (thanks Michal!) which brings us in line with Clojure on the JVM. Still discussing the implications of doing :import (CLJS-312).

David
Reply all
Reply to author
Forward
0 new messages