Can CLJS functions have metadata?

364 views
Skip to first unread message

Shantanu Kumar

unread,
Aug 25, 2012, 3:49:47 AM8/25/12
to clo...@googlegroups.com
Hi,

I noticed that `with-meta` is not working on function objects in CLJS. Compilation fails with the following error:

Error: No protocol method IWithMeta.-with-meta defined for type function: function (maps, x) {
      return x;
    }

I tried it out on the REPL and found the following:

---------- BEGIN: repl-rhino ----------
ClojureScript:cljs.user> (with-meta #(do :foo) {:foo :bar})
"Error evaluating:" (with-meta (fn* [] (do :foo)) {:foo :bar}) :as "cljs.core.with_meta.call(null,(function (){\nreturn \"\\uFDD0'foo\";\n}),cljs.core.ObjMap.fromObject([\"\\uFDD0'foo\"],{\"\\uFDD0'foo\":\"\\uFDD0'bar\"}));\n"
org.mozilla.javascript.JavaScriptException: Error: No protocol method IWithMeta.-with-meta defined for type function: 
function () {
    return "\ufdd0'foo";
}
 (cljs/core.cljs#222)
at cljs/core.cljs:222 (anonymous)
at cljs/core.cljs:214 (_with_meta)
at cljs/core.cljs:806 (with_meta)
at <cljs repl>:2 (anonymous)
at <cljs repl>:2

nil
---------- END: repl-rhino ----------

Is this supposed to be a bug? I can file an issue on JIRA if so.

Shantanu

David Nolen

unread,
Aug 25, 2012, 12:36:28 PM8/25/12
to clo...@googlegroups.com
On Sat, Aug 25, 2012 at 3:49 AM, Shantanu Kumar
<kumar.s...@gmail.com> wrote:
> Hi,
>
> I noticed that `with-meta` is not working on function objects in CLJS.
> Compilation fails with the following error:
>
> Error: No protocol method IWithMeta.-with-meta defined for type function:
> function (maps, x) {
> return x;
> }

It's not currently supported. Ticket welcome. If you have ideas about
a good approach that's even better. Part of the problem is that
Clojure fns are just JS fns.

David

Shantanu Kumar

unread,
Aug 25, 2012, 5:48:06 PM8/25/12
to clo...@googlegroups.com

Storing metadata probably would need to store the fn as an internal Clojure representation, which might lead to indirection with performance implications on fn invocation?

Shantanu

Timothy Baldridge

unread,
Aug 25, 2012, 6:18:48 PM8/25/12
to clo...@googlegroups.com
It's not currently supported. Ticket welcome. If you have ideas about
a good approach that's even better. Part of the problem is that
Clojure fns are just JS fns.


Can't we just set the attribute on the function? This works under Chrome, not sure about other browsers:

z = function(x) { return x;}
function (x) { return x;}
z(1)
1
z.foo = 1
1
z
function (x) { return x;}
z.foo
1
z(1)
1


This is the approach I took for clojure-py. You can either provide a .meta() method that will be called to get the metadata, or you can put ._meta on your object and that attribute will be set/retrieved instead. Of course, this isn't immutable, but it would support meta and alter-meta.



David Nolen

unread,
Aug 26, 2012, 10:10:40 AM8/26/12
to clo...@googlegroups.com
--
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

Yes something like that could work. Though picking a more obscure property name is probably best.

David 

David Nolen

unread,
Aug 29, 2012, 10:50:31 AM8/29/12
to clo...@googlegroups.com
Oops, actually the mutability is a showstopper. Looks like we need a
fn wrapper type which has the necessary extra field.

David

Brandon Bloom

unread,
Nov 21, 2012, 7:36:46 PM11/21/12
to clo...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages