How to instantiate Path object using clojure
Asked Answered
E

1

13

since a Path class does not have public constructor ,so path object are created using get factory method in Paths object.

e.g

Path p2 = Paths.get("/home/admin","Migrations","/blog/tables/6-rating.xml");

//or

Path p2 = Paths.get(new URI("file://home/debianaut/Migrations/blog.sakhunzai/tables/6-rating.xml"));

how we can do this in clojure way ?

Elkin answered 5/8, 2014 at 18:9 Comment(2)
(Paths/get "/home/admin" "Migrations" "/blog/tables/6-rating.xml") CompilerException java.lang.IllegalArgumentException: No matching method: get, compiling:(NO_SOURCE_PATH:5:2) Elkin
(Paths/get "/home/debianaut/Migrations/blog.sakhunzai/tables/6-rating.xml") ClassCastException java.lang.String cannot be cast to java.net.URI user/eval5598 (NO_SOURCE_FILE:11)Elkin
C
14
user> (java.nio.file.Paths/get "/home/justin" (into-array [".lein" "profiles.clj"]))
#<UnixPath /home/justin/.lein/profiles.clj>

varargs java methods need an array containing all remaining args as their final argument.

The first string outside the array is needed in order for the method dispatch to match the right method.

For completeness, here is an example using a URI (much more straightforward):

user> (java.nio.file.Paths/get (java.net.URI. "file:///home/justin"))
#<UnixPath /home/justin>
Crotchet answered 5/8, 2014 at 18:25 Comment(1)
"It is still true that multiple arguments must be passed in an array, but the varargs feature automates and hides the process." docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html clojure does not compile to java, and it simply lacks that feature that the java compiler provides.Crotchet

© 2022 - 2024 — McMap. All rights reserved.