Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

questions on the source of string?

Tags:

clojure

I was looking through the src of string? fn and had the a few questions. Below is the source of the string? fn -

(def
 ^{:arglists '([x])
   :doc "Return true if x is a String"
   :added "1.0"
   :static true}
 string? (fn ^:static string? [x] (instance? String x)))
  1. What does giving a fn static metadata do?
  2. Why is the static metadata given two times, shouldnt it be enough to specify it either for the fn or for the var?
  3. Why does the anonymous fn have a name ?
like image 363
murtaza52 Avatar asked Apr 11 '13 07:04

murtaza52


1 Answers

For answers to 1. and 2., see d.j.sheldrick's comment on the question. Answer to 3.:

Firstly, although this is not relevant here, named functions can refer to themselves by their name. This allows them to return themselves as values or to call themselves through the usual call mechanism rather than recur to the top. Importantly, this is the correct self-call strategy for functions generating lazy seqs; I've gone into the reasons why in an earlier SO answer (see the part after the "How come you can wrap recursive calls in a lazy sequence..." block quote).

Secondly, functions are compiled to JVM classes. The classes are named by the Clojure compiler based on the namespace in which the function is defined if the function is unnamed; otherwise the name is used to generate a more meaningful name for the class. This is useful for debugging, since it makes stack traces more intelligible.

like image 175
Michał Marczyk Avatar answered Sep 21 '22 07:09

Michał Marczyk