Hi,
I use deftype to declare shape of user input data and such "user-
validation" type looks like this for example:
(deftype positive-integer ()
`(and integer (satisfies positivep)))
Then I use (typep some-user-input 'positive-integer) as a test whether
the input is correct which works fine, but in case when the input is
not correct (somebody supplies value :asdf), I would like to show a
error message like:
"Value SLOTS-COUNT (:ASDF) is not of type of (AND INTEGER (SATISFIES
POSITIVEP)))"
(Note that "users" are in my case common-lisp programmers (hmm, me :)
which will find such message helpful - it's part of DSL)
My problem is, that I don't know how to get a form which describes the
type. I need a function like 'type-form' which when called like:
(type-form 'positive-integer)
will result into it's definition:
(AND INTEGER (SATISFIES POSITIVEP)))
Is this possible directly in CL or do I need to wrap it in simple
macro which will store the form in itself (like documentation slot of
type or hashtable?)
Regards,
Karol
On Apr 5, 1:04 am, Jeff <········@gmail.com> wrote:
> You can always do (assert (predicate-p value) () "Message to print if
> assertion fails").
Yes, but that way I still don't get the form of the type..
Karol
Karol Skocik <············@gmail.com> writes:
> Hi,
> I use deftype to declare shape of user input data and such "user-
> validation" type looks like this for example:
>
> (deftype positive-integer ()
> `(and integer (satisfies positivep)))
>
> Then I use (typep some-user-input 'positive-integer) as a test whether
> the input is correct which works fine, but in case when the input is
> not correct (somebody supplies value :asdf), I would like to show a
> error message like:
>
> "Value SLOTS-COUNT (:ASDF) is not of type of (AND INTEGER (SATISFIES
> POSITIVEP)))"
>
> (Note that "users" are in my case common-lisp programmers (hmm, me :)
> which will find such message helpful - it's part of DSL)
>
> My problem is, that I don't know how to get a form which describes the
> type. I need a function like 'type-form' which when called like:
>
> (type-form 'positive-integer)
>
> will result into it's definition:
>
> (AND INTEGER (SATISFIES POSITIVEP)))
>
> Is this possible directly in CL or do I need to wrap it in simple
> macro which will store the form in itself (like documentation slot of
> type or hashtable?)
The later.
But isn't POSITIVE-INTEGER enough description?
Note also that for this simple type, you can define it as:
(deftype positive-integer () '(integer 0))
and you will probably get more informative an error message, such as:
debugger invoked on a TYPE-ERROR in thread #<THREAD "initial thread"
{A72B589}>: The value -1 is not of type UNSIGNED-BYTE.
--
__Pascal Bourguignon__ http://www.informatimago.com/
NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.
On Apr 5, 4:30 am, Pascal Bourguignon <····@informatimago.com> wrote:
> Karol Skocik <············@gmail.com> writes:
> > Hi,
> > I use deftype to declare shape of user input data and such "user-
> > validation" type looks like this for example:
>
> > (deftype positive-integer ()
> > `(and integer (satisfies positivep)))
>
> > Then I use (typep some-user-input 'positive-integer) as a test whether
> > the input is correct which works fine, but in case when the input is
> > not correct (somebody supplies value :asdf), I would like to show a
> > error message like:
>
> > "Value SLOTS-COUNT (:ASDF) is not of type of (AND INTEGER (SATISFIES
> > POSITIVEP)))"
>
> > (Note that "users" are in my case common-lisp programmers (hmm, me :)
> > which will find such message helpful - it's part of DSL)
>
> > My problem is, that I don't know how to get a form which describes the
> > type. I need a function like 'type-form' which when called like:
>
> > (type-form 'positive-integer)
>
> > will result into it's definition:
>
> > (AND INTEGER (SATISFIES POSITIVEP)))
>
> > Is this possible directly in CL or do I need to wrap it in simple
> > macro which will store the form in itself (like documentation slot of
> > type or hashtable?)
>
> The later.
>
> But isn't POSITIVE-INTEGER enough description?
>
> Note also that for this simple type, you can define it as:
>
> (deftype positive-integer () '(integer 0))
>
> and you will probably get more informative an error message, such as:
>
> debugger invoked on a TYPE-ERROR in thread #<THREAD "initial thread"
> {A72B589}>: The value -1 is not of type UNSIGNED-BYTE.
>
> --
> __Pascal Bourguignon__ http://www.informatimago.com/
>
> NOTE: The most fundamental particles in this product are held
> together by a "gluing" force about which little is currently known
> and whose adhesive power can therefore not be permanently
> guaranteed.
The problem is that I do the checks in macros, and I don't want to do
(error ....) in macro because that will result in compilation error
without better information what went wrong... I want to test it and
emit form `(error "some slot somewhere is not of type ~a" '<some-
deftype-form-here>)
But I decided that since the users of that macros are just 2 CL
programmers, I will be spartan in error messages :)
Karol
Karol Skocik <············@gmail.com> writes:
> Hi,
> I use deftype to declare shape of user input data and such "user-
> validation" type looks like this for example:
>
> (deftype positive-integer ()
> `(and integer (satisfies positivep)))
>
> Then I use (typep some-user-input 'positive-integer) as a test whether
> the input is correct which works fine, but in case when the input is
> not correct (somebody supplies value :asdf), I would like to show a
> error message like:
>
> "Value SLOTS-COUNT (:ASDF) is not of type of (AND INTEGER (SATISFIES
> POSITIVEP)))"
>
> (Note that "users" are in my case common-lisp programmers (hmm, me :)
> which will find such message helpful - it's part of DSL)
>
A possible compromise use the argument to deftype
CL-USER> (deftype integer-and (predicate)
`(and integer (satisfies ,predicate)))
INTEGER-AND
CL-USER> (let ((x -4))
(check-type x (integer-and plusp))
(sqrt (/ x)))
Type a form to be evaluated:
16
0.25
Alan Crowe
Edinburgh
Scotland
On Apr 5, 2:03 pm, Alan Crowe <····@cawtech.freeserve.co.uk> wrote:
> Karol Skocik <············@gmail.com> writes:
> > Hi,
> > I use deftype to declare shape of user input data and such "user-
> > validation" type looks like this for example:
>
> > (deftype positive-integer ()
> > `(and integer (satisfies positivep)))
>
> > Then I use (typep some-user-input 'positive-integer) as a test whether
> > the input is correct which works fine, but in case when the input is
> > not correct (somebody supplies value :asdf), I would like to show a
> > error message like:
>
> > "Value SLOTS-COUNT (:ASDF) is not of type of (AND INTEGER (SATISFIES
> > POSITIVEP)))"
>
> > (Note that "users" are in my case common-lisp programmers (hmm, me :)
> > which will find such message helpful - it's part of DSL)
>
> A possible compromise use the argument to deftype
>
> CL-USER> (deftype integer-and (predicate)
> `(and integer (satisfies ,predicate)))
> INTEGER-AND
> CL-USER> (let ((x -4))
> (check-type x (integer-and plusp))
> (sqrt (/ x)))
> Type a form to be evaluated:
> 16
> 0.25
>
> Alan Crowe
> Edinburgh
> Scotland
Thats nice trick, thanks!
Karol