Name

about:prototypes

Description

A prototype object is a lightweight object based on DynamicObject. It is also a class, i.e. you can derive a new prototype object from an existing prototype object.

Prototype objects can be constructed by the lisp reader as a literal or by the function new at run-time.

When the literal expression or the new function call has an even number of arguments, the arguments are key-value pairs. The key can be given as a keyword, symbol or string. The key's string representation is the real key.

user 1 > (var a { :name b :city "amsterdam" })
it: #s( :city "amsterdam"
        :name b )
user 2 > (var b #s( name b city "amsterdam" ))
it: #s( :city "amsterdam"
        :name b )
user 3 > (.name a)
it: b
user 4 > (.city a)
it: "amsterdam"
user 5 > (.telephone a)
it: null
user 6 > (var x "mies")
it: "mies"
user 7 > (var c (new :name x :city "amsterdam"))
it: #s( :city "amsterdam"
        :name "mies" )
user 8 > (.name c)
it: "mies"
user 9 > (.city c)
it: "amsterdam"
user 10 > (.telephone c)
it: null

When the number of arguments is odd, the first argument must be either a type specifier or a list of type specifiers. A type specifier is either a prototype object or the name of a type created by deftype, defclass, defstruct or import.

In second part of the next example every argument is a constant: b is the name of a type.

user 1 > (deftype b #s( :country "netherlands" ))
it: #s(b :country "netherlands" )
user 2 > #s(b)
it: #s(b :country "netherlands" )
user 3 > #s(b name "john" city "amsterdam" )
it: #s(b :city "amsterdam"
         :country "netherlands"
         :name "john" )

In the second part of the next example every argument is evaluated: b is the name of a variable with a prototype value. The last two lines show that a prototype object can be used as a function.

user 1 > (def b (new :country "netherlands"))
it: b
user 2 > (def a (new b
                     :name "john"
                     :city "amsterdam"))
it: a
user 3 > a
it: #s( :city "amsterdam"
        :country "netherlands"
        :name "john" )
user 4 > (.name a)
it: "john"
user 5 > (.nam a)
it: null
user 6 > (elt a "name")
it: "john"
user 7 > (elt a :name)
it: "john"
user 8 > (elt a 'name)
it: "john"
user 9 > (.city a)
it: "amsterdam"
user 10 > (.country a)
it: "netherlands"
user 11 > (prototype:keys a)
it: ("name" "city" "country")
user 12 > (prototype:get-parents a)
it: (#s(:country "netherlands"))
user 13 > (funcall a "city")
it: "amsterdam"
user 14 > (map a '(city name))
it: ("amsterdam" "john")

defclass defstruct deftype new prototype:get-parents prototype:get-type-specifier prototype:has-property prototype:keys prototype:new prototype:set-parents