ANN: ClojureScript property access syntax

643 views
Skip to first unread message

Fogus

unread,
Jan 13, 2012, 1:09:10 PM1/13/12
to Clojure
I've just merged to ClojureScript master the new property lookup
syntax as outlined in the ticket http://dev.clojure.org/jira/browse/CLJS-89
and the design page at http://dev.clojure.org/display/design/Unified+ClojureScript+and+Clojure+field+access+syntax.
This is a potentially *breaking* change to existing ClojureScript
apps. To help mitigate moving to the new syntax, the ClojureScript
compiler will print a warning any time it encounters a dot call in the
form of (.foo obj) or (. obj foo). This is a temporary warning and
due for removal sometime before ClojureScript comes out of alpha. For
those curious, the change is summarized as follows:

(.p o) ;=> a method call

(.m o <args>) ;=> a method call

(. o p) ;=> a method call

(. o p <args>) ;=> a method call

(. o (m)) ;=> a method call

(. o (m <args>)) ;=> a method call

(. o -p) ;=> a property access

(.-p o) ;=> a property access

ClojureScript will now stick strictly to these semantics. As an added
bonus the latest Clojure alpha release (1.4.0-alpha4) contains a *non-
breaking* addition to Clojure to allow the same kind of property-
specific access syntax. Nothing else regarding Clojure's dot interop
form has changed.

I will take it on myself to update the ClojureScript One source code
to see if there are any further complications to migration that I
might have missed. I will report my findings in this thread.

Thanks

Fogus

unread,
Jan 13, 2012, 4:22:04 PM1/13/12
to Clojure
Notes from the trenches.

I successfully migrated both ClojureScript One and Domina to the new
syntax in about ~35 minutes (including tests). I had an unfair
advantage having worked on the ClojureScript change, but for your own
purposes the following techniques for migration should help:

- length and innerHTML properties are prevalent and should all change
to -length and -innerHTML outright

- Using the aforementioned warning messages displayed by the
ClojureScript compiler will help to pinpoint possible old-style
property accesses. It will not always be accurate in identifying a
prop lookup, but it helps to double check.

- Calls to set! take a "place" as their first argument. These are
often property slots of the form (set! (. an-obj -aprop) 42). These
"places" should be double checked.

- As a sanity check I checked every dot macro call [1] and just made
sure that a property access was formed properly.

And that's it.
Enjoy


[1]: i.e. I searched for "(\." in Emacs and looked at the found code.

Fogus

unread,
Jan 13, 2012, 4:24:39 PM1/13/12
to Clojure
Reply all
Reply to author
Forward
0 new messages