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 > (var a { :name b :city "amsterdam" })
it: a
user > (var b #s( name b city "amsterdam" ))
it: b
user > (.name a)
it: b
user > (.city a)
it: "amsterdam"
user > (.telephone a)
it: null
user > (var x "mies")
it: x
user > (var c (new :name x :city "amsterdam"))
it: c
user > (.name c)
it: "mies"
user > (.city c)
it: "amsterdam"
user > (.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 > (deftype b #s( :country "netherlands" ))
it: #s(b :country "netherlands" )
user > #s(b)
it: #s(b :country "netherlands" )
user > #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 > (def b (new :country "netherlands"))
it: b
user > (def a (new b
                   :name "john"
                   :city "amsterdam"))
it: a
user > a
it: #s( :city "amsterdam"
        :country "netherlands"
        :name "john" )
user > (.name a)
it: "john"
user > (.nam a)
it: null
user > (elt a "name")
it: "john"
user > (elt a :name)
it: "john"
user > (elt a 'name)
it: "john"
user > (.city a)
it: "amsterdam"
user > (.country a)
it: "netherlands"
user > (prototype:keys a)
it: ("name" "city" "country")
user > (prototype:get-parents a)
it: (#s(:country "netherlands"))
user > (funcall a "city")
it: "amsterdam"
user > (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