unofficial mirror of 
 help / color / mirror / Atom feed
* Exception with multiple irritants
@ 2022-11-25  0:03 Zelphir Kaltstahl
  0 siblings, 0 replies; only message in thread
From: Zelphir Kaltstahl @ 2022-11-25  0:03 UTC (permalink / raw)
  To: Guile User

Hello Guile Users!

I have a question regarding exception creation in general and maybe in specific 
about irritants.

I created a new exception type:

(library (exceptions)
   (export make-contract-violated-exception-plain
   (import (except (rnrs base) let-values)
           (only (guile)
                 lambda* λ
           (ice-9 exceptions))

   ;; Create a custom exception type, to make it clearer,
   ;; that a contract failed, and not only an arbitrary
   ;; assertion.
   (define &contract-violated
      ;; name of the new exception type
      ;; parent exception type
      ;; list of values the constructor of the exception
      ;; takes and their names in the record

   (define make-contract-violated-exception-plain
     ;; record-constructor is a procedure, which will return
     ;; the constructor for any record.
      ;; Create an exception type, which is a record. This
      ;; record has a constructor, which we can name using
      ;; define for example.

   (define contract-violated-exception?
     (exception-predicate &contract-violated))

   (define make-exception-contract-violated-compound
     (λ (message origin irritants)
        (make-exception-with-message message)
        (make-exception-with-origin origin)
        (make-exception-with-irritants irritants)))))

However, I realized, when a contract is violated, it would be nice to not only 
see the violated contract or condition as irritants, but also the values of the 
irritants. So I simply tried adding them as well in the exception:

(define make-exception-contract-violated-compound
     (λ (message origin irritants irritant-values)
        (make-exception-with-message message)
        (make-exception-with-origin origin)
        (make-exception-with-irritants irritants)
        (make-exception-with-irritants irritant-values))))

Note, that now I have 2 times `make-exception-with-irritants` in there. This 
does not cause an error and `exception-irritants` still returns the first 
irritants, so my tests also all still pass, as I have not tested for there not 
to be other exception attributes.

For example a violated exception example could look like this:

(define-with-contract bla
   (require (> foo 10))
   (ensure (> <?> 0))
   (λ (foo)
     (- 20 foo)))

(bla 10)

ice-9/boot-9.scm:1685:16: In procedure raise-exception:
   1. &contract-violated
   2. &message: "contract violated"
   3. &origin: bla
   4. &irritants: (> foo 10)
   5. &irritants: (> 10 10)

Nice! Now I have the value of `foo` in this case as well and that could be 
useful information in cases, when I get a violated contract unexpectedly.

However, having irritants twice seems a bit weird. Is this something, that is 
safe to do? Something expected and probably unchanging in future versions of GNU 
Guile? Or does it merely work by chance?

I could always make another exception type like "exception-with-irritant-values" 
or something and use that, instead of a second "with irritants" call.

Best regards,


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-11-25  0:03 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-25  0:03 Exception with multiple irritants Zelphir Kaltstahl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).