Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we test whether something is a reference?

Tags:

clojure

For now I'm using this:

(instance? clojure.lang.IDeref x)

...but I suspect there might be a better/more idiomatic way to do this.

like image 378
Stathis Sideris Avatar asked Sep 03 '11 13:09

Stathis Sideris


People also ask

How do you check references in text?

For the APA Citation Checker to do its job, citations in your paper should roughly follow APA Style guidelines, meaning: In-text citations follow the author-date format: (Smith, 2020) or Smith (2020). The reference list gives information about the author(s), publication date, and title of each source.

What do you mean by reference check?

A reference check is when an employer contacts a job applicant's previous employers, schools, colleges, and other sources to learn more about his or her employment history, educational background, and qualifications for a job.


1 Answers

This is incorrect, you are checking if the object x implements the IDeref interface, which simply means you can dereference the object with the @ symbol. What you want is this:

 (instance? clojure.lang.Ref x)

EDIT:

(Adjusting for comments).

You can do what you suggested but this has the disadvantage of classifying objects made by other users that extend IDeref to be considered a reference type. Also consider that vars also behave as reference types but do not use the IDeref interface.

There are two good options here. You can either write a function that uses an or statement:

 (def ref? [x] 
    (or (instance? clojure.lang.Ref x)
        (instance? clojure.lang.Agent x)
        ...))

Or you can use protocols to define a new predicate. This has the advantage of being extensible.

 (defprotocol Ireference? (reference? [this]))

 (extend-type java.lang.Object Ireference? (reference? [this] false))
 (extend-type nil Ireference (reference? [this] false))
 (extend-type clojure.lang.Ref Ireference? (reference? [this] true))
 (extend-type clojure.lang.Agent Ireference? (reference? [this] true))

 ;;user=> (reference? nil)
 ;;false
 ;;user=> (reference? (ref 0))
 ;;true

For another example see http://dosync.posterous.com/51626638

like image 149
bmillare Avatar answered Oct 12 '22 10:10

bmillare