Clojure variables are immutable, but with the use of an agent, the storage location can be mutated alongside the use of functions. The changed state of the agent is then returned, as we'll see in the example below.
agent-error
methodThe agent-error
method is used to throw an error during a failure of an agent in an asynchronous operation.
(agent agentname)(agent-error agentname)
This method takes one parameter, agentname
, which represents the name of the agent.
This method returns nil
when there is no error and an error exception when there is an error with an agent in an asynchronous operation.
(ns clojure.examples.example(:gen-class))(defn func [](def date (agent(java.util.Date.)))(send date + 100)(await-for 50 date)(println (agent-error date)))(func)
func
.date
variable with the agent
method and we set the agent state to a java.util.Date.
, which gives us a date.100
to date
, using the send
agent method, which will throw an error.await-for
method to enable the agent to affect the addition.agent-error
to get errors in the date
, which we print using the println
.func
.