From d550f03602a4e2b816db7a90a93813f1f96455ec Mon Sep 17 00:00:00 2001 From: ajet Date: Mon, 14 Sep 2026 08:55:02 -0900 Subject: [PATCH] clarify --- learnclj.clj | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/learnclj.clj b/learnclj.clj index 6fe1f04..f06d166 100644 --- a/learnclj.clj +++ b/learnclj.clj @@ -263,27 +263,41 @@ nil ; => nil (Null/Nothing) ;; --- 14. Java Interop --- -;; Method call +;; === Method Calls === + +;; Direct form: (. object methodName args…) (.toUpperCase "hello") ; => "HELLO" (.substring "hello" 1 3) ; => "el" + +;; Field form: (. object fieldName) — for getters (no args) (. "hello" length) ; => 5 -(.length "hello") ; => 5 (.age alice) ; => 30 + +;; Dot-dot (..): chained field access + method call (.. alice email toUpperCase) ; => "ALICE@EXAMPLE.COM" +;; Equivalent to: (. (. alice email) toUpperCase) -;; Static method -(System/getProperty "user.name") ; => "ajet" +;; === Static Methods === -;; Constructor +;; (Class/fieldName) or (Class/staticMethod args…) +(System/getProperty "user.name") ; => "ajet" (static field/property) +(System/getProperty "java.version") ; => "21.0.x" (static method call) + +;; === Constructors === + +;; (Class.) or (Class. arg1 arg2…) (java.util.Date.) ; => #inst "2026-09-14T..." (current date/time) +(java.util.Date. 2024 0 1) ; => #inst "2024-01-01T..." ;; --- 15. Threading Macros --- ;; Thread-first (->) — passes result as first arg +;; Note: (.method args) inside -> is a method call on the threaded value (-> "hello" - (.toUpperCase) - (.replace "H" "J") - (.toLowerCase)) ; => "jello" + (.toUpperCase) ; method call: .toUpperCase on "hello" + (.replace "H" "J") ; method call: .replace on "HELLO" + (.toLowerCase)) ; method call: .toLowerCase on "JELLO" + ; => "jello" ;; Thread-last (->>) — passes result as last arg (->> [1 2 3 4 5] @@ -297,7 +311,7 @@ nil ; => nil (Null/Nothing) (try (/ 1 0) (catch ArithmeticException e - (str "Math error: " (.getMessage e)))) ; => "Math error: Divide by zero" + (str "Math error: " (.getMessage e)))) ; method call: (.getMessage e) => "Math error: Divide by zero" ;; assert — throws AssertionError on failure (assert (= 2 (+ 1 1))) ; => nil (assert returns nil on success)