[racket] How to get arity of make-object?
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