Reputation: 23607
I am reading a book and I am confused on what the following code does:
(defmethod execute ((o ORDER) (l SIMUL) (e MARKETUPDATE))
(values
(list (make-TRADE :timestamp (timestamp e)
:price (price e)
:quantity (orderquantity o)))
NIL))
The source to which I got this function says that it returns two values. My question is what the body does. From my understanding, the line 3-5 creates a list with :timestamp
, :price
, :quantity
. Am I correct? What about values
, the second line? Does it return this variable too? Any summary would help. Thanks
Upvotes: 0
Views: 162
Reputation: 11
This function is a method returning two values by using the keyword values. Have a look at CLOS to better understand object orientation and at "values" for the way of returning more than one value.
Upvotes: 0
Reputation: 60074
This is a method for a generic function, specializing on arguments of types order
, simul
, and marketupdate
.
It returns 2 values
:
A list
of length 1 created by the eponymous function list
, which contains a single object of, presumably, type trade
(probably - but not necessarily - created by a defstruct
), which has slots timestamp
, price
, and quantity
.
Symbol nil
.
You can probably access the slots of the trade
using functions trade-timestamp
&c (unless the defstruct
form is non-trivial or trade
is not defined by a defstruct
at all).
Upvotes: 2
Reputation: 1541
Why the result of make-trade is wrapped in a list is hard to guess without more context, but I'd guess that an execute can be split into N trades in some scenarios.
I suspect your confusion arises almost entire because this is the first time you have encountered a use of values. Common Lisp allows functions to return multiple values. That's slightly similar to how any language allows functions to receive multiple parameters.
These multiple return values are quite efficiently implemented. Most newbies encounter multiple values for the first time on the integer division functions, which will return a remainder as their second value. Hash table look ups will return a second value to indicate if the key was actually in the table, since the value stored for the key might be nil.
In your example the second value is NIL, presumably other execute methods might return something more interesting - for example where in the update Q the order was place, or an error code if something goes wrong. Of course checking out the manual for values will be fraught with educational values(sic).
Upvotes: 0