[plt-scheme] how to creating a new "primitve type"

From: YC (yinso.chen at gmail.com)
Date: Tue May 8 16:33:26 EDT 2007

Hi Chongkai -

thanks for the helpful answers.  I am reading up on the links right now and
try to digest it.

At first I thought struct doesn't have custom constructor for validation
logic until I saw guard-proc - so that should solve the data validation
problem.  So now the question is - should the parse/validation logic reside
in readtable as well? Or is it okay to let readable construct the object,
but only receive an error from the construction?

Thanks,
yinso

On 5/8/07, Chongkai Zhu <czhu at cs.utah.edu> wrote:
>
> > You can do it in Scheme: define "ssn" as a structure in PLT Scheme, and
> > then use Readtable (chapter 11.2.8 of MzScheme) and the built-in
> > prop:custom-write structure type property to do read/write.
>
> To make "equal?" work is a little tricky. Normally you will need to
> re-define "equal?":
>
> (define (my-equal? p1 p2)
>   (if (and (ssn? p1)
>            (ssn? p2))
>       ;your code to tell whether two ssn equal?
>       (equal? p1 p2)))
>
> If you use modules, than you can creat your own language as a extension of
> MzScheme: export "my-equal?" as "equal?", and everything else from
> MzScheme.
>
> > Chongkai
> >
> > ----- Original Message -----
> > From: YC
> > To: mzscheme Mailing List
> > Sent: Tuesday, May 08, 2007 1:44 PM
> > Subject: [plt-scheme] how to creating a new "primitve type"
> >
> >
> > Hi all -
> >
> > say that I want to create a new primitive type (something like path or
> > string) so it has the first class status (for the lack of a better term)
> > as the other primitive types.  How is this done, and Is this doable only
> > in scheme or does it involve working on C code?
> >
> > Example - social security number.
> >
> >> (define s1 #ssn"123-45-6789")
> >> s1
> > #ssn"123-45-6789"
> >> (ssn->string s1)
> > "123-45-6789"
> >> (ssn? s1)
> > #t
> >> (equal? s1 #ssn"123-45-6789")
> > #t
> >> (equal? s1 #ssn"987-65-4321")
> > #f
> >
> > Any pointer is appreciated ;)  Thanks,
> > yinso
> >
> >
> >
> >
> >
> > _________________________________________________
> >  For list-related administrative tasks:
> >  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> > _________________________________________________
> >  For list-related administrative tasks:
> >  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20070508/f05b651b/attachment.html>

Posted on the users mailing list.