type-inference on namespace constants

54 views
Skip to first unread message

Michael Blume

unread,
Nov 16, 2015, 7:04:57 PM11/16/15
to cloju...@googlegroups.com
If I create a local with (let [cs (Charset/forName "UTF-8")] ...) the local cs is automatically tagged with the type Charset, and any java method calls within the block will be inferred using that information.

If I create a namespace global with (def cs (Charset/forName "UTF-8")) the var cs is *not* tagged with the type Charset. If I want to prevent reflection in calls that use it, I need to write (def ^Charset cs (Charset/forName "UTF-8")).

I have seen a lot of reflection in various libraries that went away when top-level constants were type-hinted, and which wouldn't have shown up in the first place if their types were inferred. Is there a reason for the current behavior? Should it be changed?

Andy Fingerhut

unread,
Nov 16, 2015, 9:31:38 PM11/16/15
to cloju...@googlegroups.com
Top-level constants are hard for the compiler to determine are truly constants, and not alter-var-root'ed or binding'd elsewhere?

Andy

--
You received this message because you are subscribed to the Google Groups "Clojure Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure-dev...@googlegroups.com.
To post to this group, send email to cloju...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojure-dev.
For more options, visit https://groups.google.com/d/optout.

Nicola Mometto

unread,
Nov 17, 2015, 4:01:27 AM11/17/15
to cloju...@googlegroups.com
It's a tradeoff to allow easier REPL driven development, where you can redefine a Var on the fly.
Also to account for alter-var-root
Reply all
Reply to author
Forward
0 new messages