[racket] Contract for methods with optional arguments

From: Diego Sevilla Ruiz (dsevilla at ditec.um.es)
Date: Sat Aug 31 14:02:40 EDT 2013

Ah, Matthew, thank you. Exactly what I need. I had the impression that I 
could use these, but I couldn't find examples in the documentation.

	Best regards,
	diego.

On 31/08/13 17:53, Matthew Flatt wrote:
> I think want `case->`:
>
>   (define/contract get-whatever (case->
>                                  (-> (vectorof integer?))
>                                  (-> exact-nonnegative-integer? integer?))
>     (lambda ([pos #f])
>       (if (not pos)
>           inner-vector-of-int
>           (vector-ref inner-vector-of-int pos))))
>
> You may even want to use `case-lambda` for the implementation:
>
>   (define/contract get-whatever (case->
>                                  (-> (vectorof integer?))
>                                  (-> exact-nonnegative-integer? integer?))
>     (case-lambda
>      [() inner-vector-of-int]
>      [(pos) (vector-ref inner-vector-of-int pos)]))
>
>
> At Sat, 31 Aug 2013 13:39:30 +0200, Diego Sevilla Ruiz wrote:
>> Hi all:
>>
>> 	I have encountered a situation in which I don't know how to specify the
>> contract for a method that:
>>
>> 1. Has a final optional argument
>> 2. The value of the optional argument makes the method change the return
>> type of the method.
>>
>> It is similar to this:
>>
>> (define/public (get-whatever (pos #f))
>>     (if (not pos)
>>         inner-vector-of-int
>>         (vector-ref inner-vector-of-int pos)))
>>
>> (that is, if you don't specify a position, return the whole vector, but if
>> you specify a position, return that position).
>>
>> 	For what I've seen in the documentation, there exists the ->i function
>> that allos the specification of named parameters, such as (not working but
>> you get the idea):
>>
>> (->i () ;; mandatory
>>        ([pos (or/c number? boolean?)])  ;; optional
>>        [result (pos) (or/c (and/c (=/c #f pos) (vectorof number?))
>>                            (and/c (number? pos) number?))])
>>
>> (I don't think the result expression is correct, but it expresses what I want).
>>
>> 	The real problem comes with methods, because I have ->m and ->dm, but
>> not
>> ->im (->dm does not allow specifying dependent previous values in the
>> result, for example, as far as I know).
>>
>> 	So the question is, how to write a method contract for this method?
>>
>> 	Thanks in advance,
>> 	diego.
>>
>> --
>> Diego Sevilla Ruiz -- http://ditec.um.es/~dsevilla/ -- dsevilla at um.es _.___
>> Dep. Ingeniería y Tecnología de Computadores, Facultad de Informática D|TEC
>> Univ.de Murcia,Campus Espinardo,30080 Murcia (SPAIN),Tel.+34868887571
>> ____________________
>>    Racket Users list:
>>    http://lists.racket-lang.org/users

-- 
Diego Sevilla Ruiz -- http://ditec.um.es/~dsevilla/ -- dsevilla at um.es _.___
Dep. Ingeniería y Tecnología de Computadores, Facultad de Informática D|TEC
Univ.de Murcia,Campus Espinardo,30080 Murcia (SPAIN),Tel.+34868887571

Posted on the users mailing list.