Clojure doto macro

I am going to name the method .toUpperCasein the macro dotoas follows, but doto returns small letters:

(doto (java.lang.String. "clojure")
       (.toUpperCase))

returns "clojure". I am doing a macro definition and the return value is the created object: (clojure.core/let [G__7359 (java.lang.String. "cojure")] (.toUpperCase G__7359) G__7359) but why am I not getting the answer in uppercase?

+4
source share
3 answers

dotois part of Clojure Java interoperability features. It is designed to make it possible to write java with soooo darn many parens. So

Foo foo = new Foo;
foo.setX().setY().makeFactory().applyPhaseOfMoon();

which has 8 guys, it becomes:

(doto foo .setY .makeFactory .applyPhaseOfMoon)

which has a total of two.

In this case, if we add to the expansion of your example:

user> (doto "hi" .toUpperCase)
"hi"

expands to:

user> (macroexpand-1 '(doto "hi" .toUpperCase))
(clojure.core/let [G__110453 "hi"]
   (.toUpperCase G__110453) 
   G__110453)

where the second line does this:

user> (.toUpperCase "hi")
"HI"

. doto, , java Clojure inorder API.

+5

:

x, , . . x.

doto , . , doto . .

+5

, , .. (https://clojuredocs.org/clojure.core/_..):

(.) , .. :

(.. System (getProperties) (get "os.name"))

:

(. (. System (getProperties)) (get "os.name"))

, .

:

(doto "clojure" .toUpperCase)
; => "clojure"

(.. "clojure" toUpperCase)
; => "CLOJURE"
+4

Source: https://habr.com/ru/post/1610043/


All Articles