|
Hi all,
TXR 114 is out.
Binaries are available from Bintray.
This release adds structures: familiar user-defined types featuring named storage locations called "slots".
TXR Lisp supports a friendly "object.slot" dot syntax for member referencing.
Object slots can hold functions, which can behave like methods if they take at least one argument: object.(func a b c) will retrieve a function from the object.func slot, and then invoke it with the arguments (object a b c). By contrast, object.[func a b c] will just call [object.func a b c].
Struct types are connected by inheritance, and this allows for methods to exhibit single-dispatch polymorphism, giving rise to conventional OOP (object-oriented programming).
Rudimentary example of OOP in TXR. Here, the base class downloader has a base class method fetch which performs three mock actions: connect, transfer and disconnect. The actions are implemented as stubs in three derived classes. The test code at the end of the sample is just a toplevel expression which instantiates the derived classes and exercises the fetch method. Note how all the methods are just lambda functions whose leftmost argument is the object.
(defstruct downloader nil
(fetch
(lambda (me)
me.(connect)
me.(transfer)
me.(disconnect))))
(defstruct (http-downloader addr user pass) downloader
addr user pass
(connect
(lambda (my)
(put-line `http: connecting to @{my.addr} as @{my.user}/@{my.pass}`)))
(transfer
(lambda (my)
(put-line "http: transferring data")))
(disconnect
(lambda (my)
(put-line `http: disconnecting from @{my.addr}`))))
(defstruct (ftp-downloader addr user pass) downloader
addr user pass
(connect
(lambda (my)
(put-line `ftp: connecting to @{my.addr} as @{my.user}/@{my.pass}`)))
(transfer
(lambda (my)
(put-line "ftp: transferring data")))
(disconnect
(lambda (my)
(put-line `ftp: disconnecting from @{my.addr}`))))
(defstruct (serial-downloader device) downloader
device
(connect
(lambda (my)
(put-line `serial: opening device @{my.device}`)))
(transfer
(lambda (my)
(put-line "serial: transferring data")))
(disconnect
(lambda (my)
(put-line `serial: disconnecting from @{my.device}`))))
(let ((fd (new (ftp-downloader "ftp://example.com/file.mp3" "joe" "letmein")))
(sd (new (serial-downloader "/dev/ttyS0")))
(hd (new (http-downloader "http://example.com/file.jpg" "bob" "g0dmode"))))
(each ((dl (list fd sd hd)))
(put-line)
dl.(fetch)))
Output:
ftp: connecting to ftp://example.com/file.mp3 as joe/letmein
ftp: transferring data
ftp: disconnecting from ftp://example.com/file.mp3
serial: opening device /dev/ttyS0
serial: transferring data
serial: disconnecting from /dev/ttyS0
http: connecting to http://example.com/file.jpg as bob/g0dmode
http: transferring data
http: disconnecting from http://example.com/file.jpg
This message was posted to the following mailing lists: | ||||
---|---|---|---|---|
TXR Users Mailing list info | Nearby messages |
Kaz'z Mailing List Archives adminstered by Kaz | Lurker (version 2.3, substantialy hacked by Kaz). Icon artwork from VisualPharm. |