<HTML><BODY>I don't control class creation.<br>I need to make a wrapper around make-object and attach contract to the wrapper.<br><br>Now I have<br>(provide/contract<br><span class="p"> [</span><span class="nf">read-object</span> <span class="p">(</span><span class="nf">->i</span> <span class="p">([</span><span class="nf">binary-class</span> <span class="p">(</span><span class="nf">implementation?/c</span> <span class="nv">binary<%></span><span class="p">)]</span><div class="line" id="LC146"> <span class="p">[</span><span class="nf">port</span> <span class="nv">input-port?</span><span class="p">])</span></div><div class="line" id="LC147"> <span class="kd">#:rest</span> <span class="p">[</span><span class="nf">args</span> <span class="nv">list?</span><span class="p">]</span></div><div class="line" id="LC148"> <span class="p">[</span><span class="nf">result</span> <span class="p">(</span><span class="nf">binary-class</span><span class="p">)</span> <span class="p">(</span><span class="nf">is-a?/c</span> <span class="nv">binary-class</span><span class="p">)])])<br><br>I cannot control number of args. Now, when error encountered I have confusing error message mentioning "instantiate".</span><br><br>Thu, 5 Jun 2014 12:13:26 -0400 от Matthias Felleisen <matthias@ccs.neu.edu>:<br></div>
<blockquote style="margin: 10px; padding: 0px 0px 0px 10px; border-left-color: rgb(8, 87, 166); border-left-width: 1px; border-left-style: solid;">
<div>
<div class="js-helper js-readmsg-msg">
<style type="text/css"></style>
<div>
<base href="https://e.mail.ru/" target="_self">
<div id="style_14019848050000000307_BODY"><br>
Here is the pattern I recommend: <br>
<br>
Welcome to Racket v6.0.1.11.<br>
> (define (create-c #:x [x 0]) (new c% [x x]))<br>
> (define c% (class object% (init-field x) (super-new)))<br>
<br>
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: <br>
<br>
> (create-c)<br>
(object:c% ...)<br>
> (create-c #:x 10)<br>
(object:c% ...)<br>
> (procedure-arity create-c)<br>
0<br>
> (procedure-keywords create-c)<br>
'()<br>
'(#:x)<br>
<br>
<br>
Yes, one could argue that this is a poor man's substitute for missing class reflection. -- Matthias<br>
<br>
<br>
<br>
<br>
<br>
<br>
On Jun 5, 2014, at 11:32 AM, Roman Klochkov <<a href="/compose?To=kalimehtar@mail.ru">kalimehtar@mail.ru</a>> wrote:<br>
<br>
> For any procedure I can use procedure-arity. But how to get the number of init arguments for a class?<br>
> <br>
> 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 ...)<br>
> instead of the function.<br>
> <br>
> <br>
> -- <br>
> Roman Klochkov<br>
> ____________________<br>
> Racket Users list:<br>
> <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
<br>
</div>
<base href="https://e.mail.ru/" target="_self">
</div>
</div>
</div>
</blockquote>
<br>
<br>-- <br>Roman Klochkov<br></BODY></HTML>