Ben Mabey <b...@benmabey.com> writes:

Hi Ben,
> Does the new primitive support added in 1.3 extend to anonymous
> functions?

I guess, it should.  At least they are not disregarded...

--8<---------------cut here---------------start------------->8---
user> (def with-def (fn ^float [^float x]
                        (+ x 0.5)))
;Only long and double primitives are supported
;  [Thrown class java.lang.IllegalArgumentException]
; Evaluation aborted.
--8<---------------cut here---------------end--------------->8---

> If so, I am doing something wrong because I can't get them to work:
>
> (definterface IPrimitiveTester
>   (getType [^int x])
>   (getType [^long x])
>   (getType [^float x])
>   (getType [^double x])
>   (getType [^Object x]))
>
> (deftype PrimitiveTester []
>   IPrimitiveTester
>   (getType [this ^int x] :int)
>   (getType [this ^long x] :long)
>   (getType [this ^float x] :float)
>   (getType [this ^double x] :double)
>   (getType [this ^Object x] :object))
>
> (defmacro pt [x]
>   `(.getType (PrimitiveTester.) ~x))
>
> (defn with-defn ^double [^double x]
>   (+ x 0.5))
>
> (def with-def (fn ^double [^double x]
>                 (+ x 0.5)))
>
> (pt (with-defn 1.0)) ; => :double
> (pt (with-def 1.0)) ; => :object
>
> So.. is the above user error, known limitation, bug, or new issue?

Looks like a bug to me.  And it's pretty strange, because defn expands
into a (def foo (fn ...)) anyway...

Oh, you can add the return type metadata to the def-ed var like so:

--8<---------------cut here---------------start------------->8---
user> (def ^{:tag Double/TYPE}
            with-def (fn  [^double x]
                        (+ x 0.5)))
#'user/with-def
user> (pt (with-def 3))
:double
--8<---------------cut here---------------end--------------->8---

But OTOH, the metadata of `with-defn' does not have :tag.  And basically
it shouldn't, because with overloaded arities, the return type may
differ between versions...

Bye,
Tassilo

-- 
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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to