Saturday, January 8, 2011

record type declarations

I finally needed to declare record types in some complex data structures, so I spent about 10 minutes extending the type parser to support it. The syntax is IMHO obvious.

I was pleasantly surprised to see how easy it was to add this feature, and all the testing went quickly and as expected. I think this is all a 'side-effect' (pun intended) of a relatively complete and sane type system.

Here's what the syntax looks like:

;; -*- Mode: Irken -*-

(include "lib/core.scm")
(include "lib/pair.scm")
(include "lib/string.scm")

;; declare a new datatype <thing> with only one alternative <t>
;;   which consists of an open record type... i.e., a record that
;;   may contain other unknown fields.
(datatype thing
  (:t {x=int y=char ...})
  )

(define (test1)
  (thing:t {x=3 y=#\b z=9})
  )

(define (test2)
  (thing:t {x=4 y=#\c z=#\a a=#t b=#f}))

;; make sure it still works with *no* extra fields
(define (test3)
  (thing:t {x=4 y=#\a}))

(printn (test1))
(printn (test2))
(printn (test3))

No comments:

Post a Comment