Thank you, Marc!

I will try to use your example to develop more general "instance" function.

Sincerely,
  Ru
воскресенье, 20 октября 2019 г., 15:35:48 UTC+3 пользователь mlimotte 
написал:
>
> Hey Ru,
>
> I'd also add that `new` is a special form, not a function.  It is 
> evaluated at compile-time, which is why the macro doesn't work.  You can 
> also try macro-expansion to see what's going on:
>
>
> (defn itest [x] (macroexpand-1 '(instance x)))
> => #'user/itest
> (itest java.lang.String)
> => (new x)
>
>
> So, after macro-expansion, this would be like typing the following code:
>
> (defn itest [x] (new x))
> Syntax error (IllegalArgumentException) compiling new at [...]
> Unable to resolve classname: x
>
>
> This doesn't work as a straight up function, so it won't work as a macro. 
> Macros are "shortcuts".  They won't let you do anything you couldn't do 
> with "regular" code.
>  
> Finally, as Chris suggested above, reflection is probably what you want.  
> Here's a sample to get you started:
>
> (defn itest [x & args] 
>   (let [cnstr (.getConstructor x (into-array java.lang.Class [String]))]
>     (.newInstance cnstr (into-array java.lang.Object args))))
> => #'user/itest
> (itest java.lang.String "Hello")
> => "Hello"
>
>  
> This is only a start though, because as written (line 2 in particular), it 
> only works for constructors that take exactly one String argument.  To do 
> this for real, you would need to build the [String] array dynamically by 
> introspecting the args list.
>
> Marc
>
>
>
>
> On Sat, Oct 19, 2019 at 3:02 PM <ri...@chartbeat.com <javascript:>> wrote:
>
>> Macros also allow you to create a dsl to make your code easier to use or 
>> understand by obfuscating away bits. For example a macro that times a 
>> function call by proxying the call or wrapping a call to defn.
>>
>> That said, I consider many uses of macros that I’ve seen to be 
>> antipatterns that lead to hard to understand code. They should almost 
>> always be considered a last resort. 
>>
>>
>> On Oct 19, 2019, at 12:12 PM, Matching Socks <phil...@gmail.com 
>> <javascript:>> wrote:
>>
>> Macros manipulate program symbols.  Macros fill the role that is filled 
>> by Perl scripts in the Java world, when they pre-process stuff (a database 
>> schema, or a gui model, for example) into actual Java code before you 
>> compile it.  If a task could not be solved by pre-processing the source 
>> code before the program starts to run, then it is not a task for a macro.  
>>
>> On the bright side, using Java reflection is less tedious in Clojure than 
>> it is in Java.
>>
>> -- 
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To post to this group, send email to clo...@googlegroups.com 
>> <javascript:>
>> Note that posts from new members are moderated - please be patient with 
>> your first post.
>> To unsubscribe from this group, send email to
>> clo...@googlegroups.com <javascript:>
>> For more options, visit this group at
>> http://groups.google.com/group/clojure?hl=en
>> --- 
>> You received this message because you are subscribed to the Google Groups 
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to clo...@googlegroups.com <javascript:>.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/clojure/2c56ec62-9908-40b8-aacc-aecaf157049c%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/clojure/2c56ec62-9908-40b8-aacc-aecaf157049c%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>> -- 
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To post to this group, send email to clo...@googlegroups.com 
>> <javascript:>
>> Note that posts from new members are moderated - please be patient with 
>> your first post.
>> To unsubscribe from this group, send email to
>> clo...@googlegroups.com <javascript:>
>> For more options, visit this group at
>> http://groups.google.com/group/clojure?hl=en
>> --- 
>> You received this message because you are subscribed to the Google Groups 
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to clo...@googlegroups.com <javascript:>.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/clojure/E0B8D5A1-AC56-4FD6-BAC9-220E628FC510%40chartbeat.com
>>  
>> <https://groups.google.com/d/msgid/clojure/E0B8D5A1-AC56-4FD6-BAC9-220E628FC510%40chartbeat.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/clojure/da73be2b-305b-4032-828f-a417c7ba52de%40googlegroups.com.

Reply via email to