ClojureScript

Implement Var form, var-get, and var? in CLJS

Details

  • Type: Enhancement Enhancement
  • Status: Open Open
  • Priority: Minor Minor
  • Resolution: Unresolved
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
  • Patch:
    Code and Test

Description

See discussion of Vars in CLJS here: http://dev.clojure.org/display/design/Dynamic+Binding
My goal is to eventually implement dynamic scope capture, via bound-fn and friends. The attached patch is a step in that direction.

This patch adds support for parsing the (var foo) form. The #' reader macro is provided by JVM Clojure.

#'foo emits code to construct a Var object. In this patch, each invocation of 'var will create a unique Var object. This means they are '= by fully qualified symbol, but not 'identical?. Simple memoization would fix that, but I'm not going to bother until I get to Dynamic Var objects.

The main advantage of this level of Var support is for the interactive development convenience of being able to defer dereferencing top-levels. For example, (def fog (comp f #'g)) will pick up redefinitions of 'g, but not of 'f.

Activity

Hide
Brandon Bloom added a comment -

I found two issues with this patch:

1) I never used the IVar that I declared :-P

2) (apply #'+ (range 100)) throws "Invalid arity: 101" – this is related to http://dev.clojure.org/jira/browse/CLJS-211

I'll look into fixing both. However, we should discuss supporting variable arity protocol methods...

Show
Brandon Bloom added a comment - I found two issues with this patch: 1) I never used the IVar that I declared :-P 2) (apply #'+ (range 100)) throws "Invalid arity: 101" – this is related to http://dev.clojure.org/jira/browse/CLJS-211 I'll look into fixing both. However, we should discuss supporting variable arity protocol methods...
Hide
David Nolen added a comment -

any support for reified vars is a low priority for the foreseeable future.

Show
David Nolen added a comment - any support for reified vars is a low priority for the foreseeable future.
David Nolen made changes -
Field Original Value New Value
Priority Major [ 3 ] Minor [ 4 ]
Hide
Brandon Bloom added a comment -

Reified vars (and binding frames!) are a requirement for a CPS transform to be correct with respect to dynamic bindings.

Show
Brandon Bloom added a comment - Reified vars (and binding frames!) are a requirement for a CPS transform to be correct with respect to dynamic bindings.
Hide
Tom Jack added a comment -

+1 towards bound-fn and friends. I had an explanation written here but now I see "async Javascript (ie. nearly all Javascript) makes the binding macro effectively useless without bound-fn". Indeed.

Show
Tom Jack added a comment - +1 towards bound-fn and friends. I had an explanation written here but now I see "async Javascript (ie. nearly all Javascript) makes the binding macro effectively useless without bound-fn". Indeed.
Hide
Herwig Hochleitner added a comment -

bound-fn could also be implemented by setting / resetting bound vars before / after the wrapped fn, similar to binding. We would just need to add tracking of the currently rebound dynamic vars.
That should also be CPS - friendly, no?

Show
Herwig Hochleitner added a comment - bound-fn could also be implemented by setting / resetting bound vars before / after the wrapped fn, similar to binding. We would just need to add tracking of the currently rebound dynamic vars. That should also be CPS - friendly, no?
Hide
Tom Jack added a comment -

I've considered that and would love to see it happen.

But what about:

(def ^:dynamic *foo* 3)
(set! *foo* 4)
(def f (bound-fn [] *foo*))
(set! *foo* 5)
(f)
Show
Tom Jack added a comment - I've considered that and would love to see it happen. But what about:
(def ^:dynamic *foo* 3)
(set! *foo* 4)
(def f (bound-fn [] *foo*))
(set! *foo* 5)
(f)

People

Vote (0)
Watch (3)

Dates

  • Created:
    Updated: