Fork me on GitHub

Is there any way to coerce types in defrecord


can I define a defrecord with particular parameter type


and even when I pass a string it will be parsed to float?


hello, I have a question more related to lein than to Clojure, so I'm not sure if it is the right place to post... I'm trying to use lein at work but we are behind some kind of crazy proxy that change ssl certificates on the fly. I was able to install lein by defining variables HTTP[S]_PROXY using curl's --insecure option. The problem now, is that lein is not using curl anymore and fails to download files because of this. So, my question is how can we tell lein to ignore certificate when downloading ?

Alex Miller (Clojure team)11:06:04

kamillelonek: the only types you can force in a defrecord are long or double via type hints on the fields: (defrecord R [^long a ^double b])

Alex Miller (Clojure team)11:06:17

everything else is an object


but even then there was no coercion

Alex Miller (Clojure team)11:06:05

well, I'm quite sure that using ^long and ^double hints will yield a Java object with long or double fields. what do you mean by coercion?


I mean implicit type conversion


without exlipicilty parsing it like Float/parseFloat


I mean String->Float case


read-string is said to be unsafe

Alex Miller (Clojure team)11:06:02

oh, no you shouldn't expect that to ever happen automatically


ok, that is reasonable

Alex Miller (Clojure team)11:06:36

the best way to do exactly that conversion is with Double/parseDouble (if you really want a primitive double) or Double/valueOf (if you're expecting a boxed number)

Alex Miller (Clojure team)11:06:50

generally in Clojure, we don't ever use the Java Float type

Alex Miller (Clojure team)11:06:42

there's very few cases when I would ever use it in Java either for that matter, the precision is too poor for almost anything useful.


I actually used:


(defn- build-model [[name gramms-from-milliliter]]
  (let [gramms (bigdec gramms-from-milliliter)]
    (->Ingredient name gramms)))

Alex Miller (Clojure team)12:06:42

well that will work, but you get back a BigDecimal object, not a double. depending what you're doing with it, they perform a lot differently.

Alex Miller (Clojure team)12:06:43

but it has the benefit of arbitrary precision, so that may be what you want