<HTML><BODY>(define (read-object binary-class in . args)<br> (send (apply make-object binary-class args) read in))<br><br>read-object takes a class as a first arg, and additional arguments to make an object of that class.<br><br>I can't make case-> because I don't know j,ject of what (user-defined) class will be created with the function by user of my library.<br><br>This case is described in Racket Guide 7,3.9 <a href="http://docs.racket-lang.org/guide/contracts-general-functions.html#%28part._contracts-no-domain%29">http://docs.racket-lang.org/guide/contracts-general-functions.html#%28part._contracts-no-domain%29</a> , where recommended to use <a name="(form._((lib._racket/contract/base..rkt)._unconstrained-domain-~3e))" class="mceItemAnchor"></a><span title="Provided from: racket/contract/base, racket/contract, racket | Package: base"><span class="RktSym"><a class="RktStxDef RktStxLink" href="http://docs.racket-lang.org/reference/function-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._unconstrained-domain-~3e%29%29" data-mce-href="http://docs.racket-lang.org/reference/function-contracts.html#%28form._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._unconstrained-domain-~3e%29%29" data-pltdoc="x"><font color="#0066cc">unconstrained-domain-></font></a></span></span> with <span class="RktSym"><a class="RktValLink" href="http://docs.racket-lang.org/reference/procedures.html#%28def._%28%28quote._~23~25kernel%29._procedure-arity-includes~3f%29%29" data-mce-href="http://docs.racket-lang.org/reference/procedures.html#%28def._%28%28quote._~23~25kernel%29._procedure-arity-includes~3f%29%29" data-pltdoc="x"><font color="#0066cc">procedure-arity-includes?</font></a></span> , but it doesnt' work with make-object<br><br>Thu, 5 Jun 2014 14:51:21 -0400 от Matthias Felleisen <matthias@ccs.neu.edu>:<br>
<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_14019942820000000168_BODY"><br>
From what I understand now, you want a contract for a function that creates objects from a variable number of arguments. If you write your module interface like this, <br>
<br>
> #lang racket<br>
> <br>
> (provide<br>
> (contract-out<br>
> (read-object (case-><br>
> [-> 'a number? number?]<br>
> [-> 'b number? number? number?]))))<br>
> <br>
> (define read-object<br>
> (case-lambda<br>
> [(a x) x]<br>
> [(b x y) (+ x y)]))<br>
<br>
you can get checked variable-arity behavior: <br>
<br>
> Welcome to Racket v6.0.1.11.<br>
> > (require "foo.rkt")<br>
> > (read-object 'a 10)<br>
> 10<br>
> > (read-object 'b 10 20)<br>
> 30<br>
> > (read-object 'b 10)<br>
> read-object: contract violation<br>
> expected: (quote a)<br>
> given: 'b<br>
> in: the domain of<br>
> the 1st case of<br>
> (case-><br>
> (-> 'a number? number?)<br>
> (-> 'b number? number? number?))<br>
> contract from: <br>
> /Users/matthias/svn/2HtDP/foo.rkt<br>
> blaming: top-level<br>
> at: /Users/matthias/svn/2HtDP/foo.rkt:5.5<br>
> context...:<br>
> /Users/matthias/plt/racket/collects/racket/contract/private/blame.rkt:143:0: raise-blame-error16<br>
> /Users/matthias/plt/racket/collects/racket/private/misc.rkt:87:7<br>
> > (read-object 'a 10 20)<br>
> read-object: contract violation<br>
> expected: (quote b)<br>
> given: 'a<br>
> in: the domain of<br>
> the 2nd case of<br>
> (case-><br>
> (-> 'a number? number?)<br>
> (-> 'b number? number? number?))<br>
> contract from: <br>
> /Users/matthias/svn/2HtDP/foo.rkt<br>
> blaming: top-level<br>
> at: /Users/matthias/svn/2HtDP/foo.rkt:5.5<br>
> context...:<br>
> /Users/matthias/plt/racket/collects/racket/contract/private/blame.rkt:143:0: raise-blame-error16<br>
> /Users/matthias/plt/racket/collects/racket/private/misc.rkt:87:7<br>
<br>
<br>
I think you can use ->i contracts inside of the case-> clauses if you need more precision. <br>
<br>
If I am still misunderstanding your question, sorry. <br>
<br>
-- Matthias<br>
<br>
<br>
<br>
<br>
<br>
On Jun 5, 2014, at 1:41 PM, Roman Klochkov <<a href="/compose?To=kalimehtar@mail.ru">kalimehtar@mail.ru</a>> wrote:<br>
<br>
> It is not writing, but reading. I'm making uinversal library for binary data (like dbf, mp3 id3 tags, and so on) mapping to objects<br>
> <a href="https://github.com/Kalimehtar/binary-class" target="_blank">https://github.com/Kalimehtar/binary-class</a><br>
> <br>
> read-object simply<br>
> <br>
> (define (read-object binary-class in . args)<br>
> (send (apply make-object binary-class args) read in))<br>
> <br>
> But I don't like, that contract may not give correct error message for wrong nomber of items in args.<br>
> <br>
> <br>
> Thu, 5 Jun 2014 12:31:49 -0400 от Matthias Felleisen <<a href="/compose?To=matthias@ccs.neu.edu">matthias@ccs.neu.edu</a>>:<br>
> <br>
> 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<br>
> <br>
> <br>
> <br>
> On Jun 5, 2014, at 12:29 PM, Roman Klochkov <<a href="/compose?To=kalimehtar@mail.ru">kalimehtar@mail.ru</a>> wrote:<br>
> <br>
> > 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>
> > [read-object (->i ([binary-class (implementation?/c binary<%>)]<br>
> > [port input-port?])<br>
> > #:rest [args list?]<br>
> > [result (binary-class) (is-a?/c binary-class)])])<br>
> > <br>
> > I cannot control number of args. Now, when error encountered I have confusing error message mentioning "instantiate".<br>
> > <br>
> > Thu, 5 Jun 2014 12:13:26 -0400 от Matthias Felleisen <<a href="/compose?To=matthias@ccs.neu.edu">matthias@ccs.neu.edu</a>>:<br>
> > <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>
> > <br>
> > <br>
> > -- <br>
> > Roman Klochkov<br>
> <br>
> <br>
> <br>
> -- <br>
> Roman Klochkov<br>
<br>
</div>
<base href="https://e.mail.ru/" target="_self">
</div>
</div>
</div>
</blockquote>
<br>
<br>-- <br>Roman Klochkov<br></BODY></HTML>