[racket] How to get arity of make-object?

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Thu Jun 5 12:31:49 EDT 2014

Do you control the writing of objects to a port? If so, check out 'serialization.' If not, I don't think I can help you. Sorry -- Matthias



On Jun 5, 2014, at 12:29 PM, Roman Klochkov <kalimehtar at mail.ru> wrote:

> I don't control class creation.
> I need to make a wrapper around make-object and attach contract to the wrapper.
> 
> Now I have
> (provide/contract
>   [read-object (->i ([binary-class (implementation?/c binary<%>)]
>                         [port input-port?])
>                        #:rest [args list?]
>                        [result (binary-class) (is-a?/c binary-class)])])
> 
> I cannot control number of args. Now, when error encountered I have confusing error message mentioning "instantiate".
> 
> Thu, 5 Jun 2014 12:13:26 -0400 от Matthias Felleisen <matthias at ccs.neu.edu>:
> 
> Here is the pattern I recommend: 
> 
> Welcome to Racket v6.0.1.11.
> > (define (create-c #:x [x 0]) (new c% [x x]))
> > (define c% (class object% (init-field x) (super-new)))
> 
> That is, a class comes with a 'factory' definition, a function that creates instances and uses keywords similar to those used by the class initializer. If you then export these factories, you can enforce invariants and also probe the factory for the information you want: 
> 
> > (create-c)
> (object:c% ...)
> > (create-c #:x 10)
> (object:c% ...)
> > (procedure-arity create-c)
> 0
> > (procedure-keywords create-c)
> '()
> '(#:x)
> 
> 
> Yes, one could argue that this is a poor man's substitute for missing class reflection. -- Matthias
> 
> 
> 
> 
> 
> 
> On Jun 5, 2014, at 11:32 AM, Roman Klochkov <kalimehtar at mail.ru> wrote:
> 
> > For any procedure I can use procedure-arity. But how to get the number of init arguments for a class?
> > 
> > Or maybe there are any other way to make a contract, like in Guide 7.3.9, where one can compare number of arguments and arity of the function, but when using (make-object someclass ...)
> > instead of the function.
> > 
> > 
> > -- 
> > Roman Klochkov
> > ____________________
> > Racket Users list:
> > http://lists.racket-lang.org/users
> 
> 
> 
> -- 
> Roman Klochkov



Posted on the users mailing list.