Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What type is a function?

Tags:

let's try some calls to the "type" function :

user=> (type 10)
java.lang.Integer

user=> (type 10.0)
java.lang.Double

user=> (type :keyword?)
clojure.lang.Keyword

and now with an anonymous function :

user=> (type #(str "wonder" "what" "this" "is"))
user$eval7$fn__8

A) what does this mean "user$eval7$fn__8" ? B) and what type is a function ?

the source for "type" is :

user=> (source type)
(defn type
  "Returns the :type metadata of x, or its Class if none"
  {:added "1.0"}
  [x]
  (or (:type (meta x)) (class x)))
nil

so a function needs to have a specific part of meta data or be a class

checking the meta of an anonymous function yields nada :

user=> (meta #(str "wonder" "what" "this" "is"))
nil

trying a different approach :

user=> (defn woot [] (str "wonder" "what" "this" "is"))
#'user/woot
user=> (meta woot)
{:ns #<Namespace user>, :name woot}

C) seems there is some meta but i figured this is the meta of the symbol "woot", right ?

what about the second half of the "or" :

user=> (class #(str "wonder" "what" "this" "is"))
user$eval31$fn__32

user=> (class woot)
user$woot

what are these : "user$eval31$fn__32" and "user$woot" and where do they come from ?

checking out the "class" function yields:

user=> (source class)
(defn ^Class class
  "Returns the Class of x"
  {:added "1.0"}
  [^Object x] (if (nil? x) x (. x (getClass))))
nil

and further investigating yields :

user=> (.getClass #(str "wonder" "what" "this" "is"))
user$eval38$fn__39

user=> (.getClass woot)
user$woot

i don't get it. D) is this a hashcode : eval38$fn__39 ? E) is this a symbol : woot ?

F) why doesn't a function have a type ? isn't it supposed to be an IFn or something ?

like image 847
Belun Avatar asked Sep 14 '10 11:09

Belun


People also ask

What are the 4 types of function?

The types of functions can be broadly classified into four types. Based on Element: One to one Function, many to one function, onto function, one to one and onto function, into function.

Do functions have types?

Every function has a specific function type, made up of the parameter types and the return type of the function. For example: func addTwoInts(_ a: Int, _ b: Int) -> Int { return a + b.

What is the example of type function?

Python type() is a built-in function that is used to return the type of data stored in the objects or variables in the program. For example, if a variable contains a value of 45.5 then the type of that variable is float.


2 Answers

A function is of type clojure.lang.IFn, which is a Java interface.

Every Clojure function is compiled into a Java class which implements clojure.lang.IFn. The name user$eval7$fn__8 is the "binary class name" of that class, i.e., its internal name in the JVM.

like image 82
Stuart Sierra Avatar answered Jan 31 '23 22:01

Stuart Sierra


Clojure is built on the JVM.

The JVM doesn't support first-class functions, or lambdas, out of the box. Every Clojure function, once it is compiled, becomes its own anonymous class from the perspective of the JVM. Each function is, technically, it's own type.

The class it becomes implements IFn, but when you retrieve it's type, it gives you the name of the anonymous class which is different every time.

like image 32
levand Avatar answered Jan 31 '23 22:01

levand