chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] define-record with default values?


From: Thomas Chust
Subject: Re: [Chicken-users] define-record with default values?
Date: Mon, 10 Aug 2009 22:49:39 +0200

2009/8/10 Christian Kellermann <address@hidden>:
> [...]
> I'd like to define a record (srfi-9) style or similar but I'd also
> like to be able to pass a slot a default value.  Bigloo scheme for
> example has had define-structures for that. Can this be done in
> chicken as well with existing tools?
> [...]

Hello,

one possible and rather portable solution is to define the record type
as usual but also define an alternative constructor procedure that has
non-mandatory arguments:

  (module foo (make-foo foo? foo-a foo-b)
    (import scheme chicken)

    (define-record-type foo
      (%make-foo a b)
      foo?
      (a foo-a)
      (b foo-b))

    (define (make-foo #!key [a 42] [b 23])
      (%make-foo a b)))

The easiest way probably is to use the defstruct egg, though, which
internally works similar to the code above, but has a more convenient
interface:

  (module foo (make-foo foo? foo-a foo-b)
    (import scheme chicken)
    (use defstruct)

    (defstruct foo [a 42] [b 23]))


cu,
Thomas


-- 
When C++ is your hammer, every problem looks like your thumb.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]