error-on-reflection

152 views
Skip to first unread message

Phillip Lord

unread,
Dec 9, 2013, 5:11:58 AM12/9/13
to clo...@googlegroups.com

I know about *warn-on-reflection* but is there anyway that I can get an
error-on-reflection instead?

I've been type hinting my application (50% done and yes it goes faster
now), and it's a bit painful. What I would really want is to have a unit
test which fails if reflection is used.

So far, my best idea is catching standard-out and parsing it for
reflection warnings. Not ideal.

Phil

Tassilo Horn

unread,
Dec 9, 2013, 5:47:54 AM12/9/13
to Phillip Lord, clo...@googlegroups.com
philli...@newcastle.ac.uk (Phillip Lord) writes:

> I know about *warn-on-reflection* but is there anyway that I can get
> an error-on-reflection instead?

I don't think so.

> I've been type hinting my application (50% done and yes it goes faster
> now), and it's a bit painful. What I would really want is to have a
> unit test which fails if reflection is used.
>
> So far, my best idea is catching standard-out and parsing it for
> reflection warnings. Not ideal.

clojure/test/clojure/test_helper.clj already contains some helper macros
for doing that, e.g., `should-not-reflect`:

https://github.com/clojure/clojure/blob/0b73494c3c855e54b1da591eeb687f24f608f346/test/clojure/test_helper.clj#L126

One problem is, though, that you not only get reflection warnings for
your own code but also for code in dependencies. So you need to tweak
that macro with another regex that matches only reflection warnings in
your own files. But then you should be able to do something like this:

--8<---------------cut here---------------start------------->8---
(ns myproject.test
:require [clojure.test :as test])

(defmacro should-not-reflect ...)

(test/deftest no-reflection-at-all
(should-not-reflect
(do
(require 'myproject.ns1 :reload)
(require 'myproject.ns2 :reload)
...)))
--8<---------------cut here---------------end--------------->8---

HTH,
Tassilo

John D. Hume

unread,
Dec 9, 2013, 8:28:24 AM12/9/13
to clo...@googlegroups.com

Are you aware of `lein check`? We have our some of our CI builds wired to fail if that finds anything.

--
--
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
---
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 clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Phillip Lord

unread,
Dec 10, 2013, 5:27:59 AM12/10/13
to clo...@googlegroups.com

I wasn't and this is actually very useful. Thanks for the pointer.
> --

--
Phillip Lord, Phone: +44 (0) 191 222 7827
Lecturer in Bioinformatics, Email: philli...@newcastle.ac.uk
School of Computing Science, http://homepages.cs.ncl.ac.uk/phillip.lord
Room 914 Claremont Tower, skype: russet_apples
Newcastle University, twitter: phillord
NE1 7RU

Phillip Lord

unread,
Dec 10, 2013, 5:29:24 AM12/10/13
to clo...@googlegroups.com

Perfect -- I can use lein check interactively, and this in my unit
tests. It's a shame *warn-on-reflection* doesn't take a function, which
would be the general solution.

Phil

Tassilo Horn <ts...@gnu.org> writes:
> philli...@newcastle.ac.uk (Phillip Lord) writes:
>
>> I know about *warn-on-reflection* but is there anyway that I can get
>> an error-on-reflection instead?
>
> I don't think so.
>
>> I've been type hinting my application (50% done and yes it goes faster
>> now), and it's a bit painful. What I would really want is to have a
>> unit test which fails if reflection is used.
>>
>> So far, my best idea is catching standard-out and parsing it for
>> reflection warnings. Not ideal.
>
> clojure/test/clojure/test_helper.clj already contains some helper macros
> for doing that, e.g., `should-not-reflect`:
>
> https://github.com/clojure/clojure/blob/0b73494c3c855e54b1da591eeb687f24f608f346/test/clojure/test_helper.clj#L126
>
> One problem is, though, that you not only get reflection warnings for
> your own code but also for code in dependencies. So you need to tweak
> that macro with another regex that matches only reflection warnings in
> your own files. But then you should be able to do something like this:
>
>
> (ns myproject.test
> :require [clojure.test :as test])
>
> (defmacro should-not-reflect ...)
>
> (test/deftest no-reflection-at-all
> (should-not-reflect
> (do
> (require 'myproject.ns1 :reload)
> (require 'myproject.ns2 :reload)
> ...)))
>
>
> HTH,
> Tassilo
Reply all
Reply to author
Forward
0 new messages