Fork me on GitHub
#beginners
<
2015-06-30
>
kamillelonek09:06:06

Is there any way to coerce types in defrecord

kamillelonek09:06:26

can I define a defrecord with particular parameter type

kamillelonek09:06:38

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

calyce09:06:10

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

kamillelonek11:06:05

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?

kamillelonek11:06:20

I mean implicit type conversion

kamillelonek11:06:33

without exlipicilty parsing it like Float/parseFloat

kamillelonek11:06:51

I mean String->Float case

kamillelonek11:06:00

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

kamillelonek11:06:14

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.

kamillelonek12:06:48

I actually used:

kamillelonek12:06:51

(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