Clojure overloaded method resolution for Longs

前端 未结 2 1376
迷失自我
迷失自我 2020-12-16 22:01

This behavior makes no sense to me:

user=> (type 1)
java.lang.Long
user=> (type (cast Long 1))
java.lang.Long
user=> (type 1)
java.lang.Long
user=&g         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-16 22:26

    BigDecimal does not have a constructor for Long,

    BigDecimal(BigInteger val)

    core> (BigDecimal. (BigInteger/ONE))
    1M
    

    BigDecimal(BigInteger unscaledVal, int scale)

    core> (BigDecimal. BigInteger/ONE 1)
    0.1M
    

    BigDecimal(double val)

    core> (BigDecimal. (double 1))
    1M
    core> (BigDecimal. (float 1))
    1M
    (BigDecimal. Double/MIN_VALUE)
    

    BigDecimal(String val)

    core> (BigDecimal. "1")
    1M
    

    It's unclear which of these (Long. 1) matches. the clojure.core.bigdec function works on this input by passing it's input to BigDec/valueOf to create the BigDecimal

    core>  (bigdec (Long. 1))
    1M
    

    uses this call:

    (BigDecimal/valueOf (long x))
    

提交回复
热议问题