Troubleshooting: Status in Clojure

I understand that: state / does /. It creates a field, as in Java, in your class. What I do not understand, what is the meaning of this? It seems like I just see this being done with Clojure-generated classes that extend other classes. http://www.fatvat.co.uk/2009/05/clojure-and-robocode.html is one example. I don't know Java, and I'm not very good at object oriented programming. Can someone explain the point: the state for me and where does it all fit into Java interop?

Thanks a lot!

NOTE. When I say: state, I mean (: gen-class: state)

+4
source share
3 answers

I talked about this with an employee on the IRC # Clojure channel, and he told me that the main point of this is the state per copy. It makes sense.

+2
source

: state is just a way of exchanging some data between functions generated as part of a gen class. Think of it as the same as object instance data.

+3
source

More information about state and how to initialize it can be found in the article gen-class - how it works and how to use it

From the article:

  • :state defines a method that returns the state of an object.
  • :init defines the name of the initializer. This is a function that should return a vector. The first element is again the argument vector to the superclass constructor. In our case, this is just an empty vector. The second element is the state of the object.

Thus, init returns the state of the object and is called when an instance of the object is created. state is a class method, unlike a function that will return the same value that is returned as the second element in the vector returned by init .

The rest of the article shows how to use an atom to be able to change the state of an object, if necessary.

+3
source

All Articles