Can CLJS functions have metadata?

Showing 1-7 of 7 messages
Can CLJS functions have metadata? Shantanu Kumar 8/25/12 12:49 AM
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
Re: Can CLJS functions have metadata? David Nolen 8/25/12 9:36 AM
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
Re: Can CLJS functions have metadata? Shantanu Kumar 8/25/12 2:48 PM

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
Re: Can CLJS functions have metadata? tbc++ 8/25/12 3:18 PM

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.



Re: Can CLJS functions have metadata? David Nolen 8/26/12 7:10 AM
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscribe@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 
Re: Can CLJS functions have metadata? David Nolen 8/29/12 7:50 AM
Oops, actually the mutability is a showstopper. Looks like we need a
fn wrapper type which has the necessary extra field.

David
Re: Can CLJS functions have metadata? Brandon Bloom 11/21/12 4:36 PM