On Wednesday, 11 December 2013 14:50:36 UTC, Phillip Lord wrote:
>
> Mikera <[email protected] <javascript:>> writes:
>
> > On Wednesday, 11 December 2013 13:37:24 UTC, Philipp Meier wrote:
> >> Implementing a clojure protocol will give you fast dispatch on the
> first
> >> argument's type.
> >>
> >
> > Very true... it's a tradeoff:
> > - protocols allow open extension (which doesn't appear to be needed
> here?)
> > - instance? checks are even faster (although protocol dispatch is still
> > pretty fast)
> > - protocols have some other quirks (e.g. not allowing primitive
> arguments)
> >
> > In the case we have here (a small fixed number of types, no open
> dispatch
> > required) I think I would prefer instance? checks (especially given a
> nice
> > macro to remove the boilerplate).
>
>
> Yeah, unfortunately I do need dispatch on primitive arguments, so
> protocols fail for me.
>
> I've been trying to get this to work as a macro, but am having problems
> with this. The instance? check is easy to do, but adding the type hint
> is much harder; I think that the ^macro expands at read time, so before
> the macro expands, so it's not possible to construct the type hint.
>
> I've tried to rewrite this with `with-meta' but can't get this to work
> either since I think the meta is being added to the value.
>
> My hope was to get something like this...
>
> (defn ^IRI iri
> [name]
> (with-type-hint
> [name [String java.net.URL java.io.File]]
> (IRI/create name)))
>
> Am I really the first person to get this problem?
>
> Phil
>
Macros that generate type hints can get pretty ugly. I ran into the same
problem with vectorz-clj
The following pattern ended up working for me: the trick to to create a
local symbol that you can tag
(defmacro tag-symbol [tag form]
(let [tagged-sym (vary-meta (gensym "res") assoc :tag tag)]
`(let [~tagged-sym ~form] ~tagged-sym)))
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.