Can CLJS functions have metadata?

365 kali dilihat
Langsung ke pesan pertama yang belum dibaca

Shantanu Kumar

belum dibaca,
25 Agu 2012, 03.49.4725/08/12
kepadaclo...@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

belum dibaca,
25 Agu 2012, 12.36.2825/08/12
kepadaclo...@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

belum dibaca,
25 Agu 2012, 17.48.0625/08/12
kepadaclo...@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

belum dibaca,
25 Agu 2012, 18.18.4825/08/12
kepadaclo...@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

belum dibaca,
26 Agu 2012, 10.10.4026/08/12
kepadaclo...@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

belum dibaca,
29 Agu 2012, 10.50.3129/08/12
kepadaclo...@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

belum dibaca,
21 Nov 2012, 19.36.4621/11/12
kepadaclo...@googlegroups.com
Balas ke semua
Balas ke penulis
Teruskan
0 pesan baru