[racket] Typed racket and command line parameters

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sun Jun 22 13:20:36 EDT 2014

Conceptually you want to signal the error out of the 'parms' computation. 
So even if you introduce string->real as a variation on string->number, 
you probably want to catch the error it raises and re-cast it as 'params'
message. 




On Jun 22, 2014, at 12:56 PM, Manfred Lotz wrote:

> On Sun, 22 Jun 2014 11:58:59 -0400
> Sam Tobin-Hochstadt <samth at cs.indiana.edu>
> wrote:
> 
>> Fortunately, one of those casts can be eliminated with a bit of type
>> annotation on `NUM`.   Full code here:
>> https://gist.github.com/samth/48bf2ccb2c510c75017e
>> 
>> (define parms
>>  (command-line
>>   #:program "mypgm"
>>   #:once-each
>>   [("-v" "--verbose") "Show verbose output" (opt-verbose-mode #t)]
>>   [("-m" "--max-size")
>>    #{NUM : String} ("Max size." "NUM")
>>    (opt-max-size (cast (string->number NUM) Real))]))
>> 
>> Sam
>> 
> 
> This is indeed nice. Thanks for the variation to Matthias solution.
> 
> -- 
> Manfred
> 
>> 
>> On Sun, Jun 22, 2014 at 11:25 AM, Matthias Felleisen
>> <matthias at ccs.neu.edu> wrote:
>>> 
>>> I would use Real for the parameter type and then cast twice:
>>> 
>>> (define parms
>>>  (command-line
>>>   #:program "mypgm"
>>>   #:once-each
>>>   [("-v" "--verbose") "Show verbose output" (opt-verbose-mode #t)]
>>>   [("-m" "--max-size")
>>>    NUM
>>>    ("Max size." "NUM")
>>>    (opt-max-size (cast (string->number (cast NUM String)) Real))]))
>>> 
>>> The casts bring out that you are willing to sacrifice precision of
>>> error messages for concision in code.
>>> 
>>> -- Matthias
>>> 
>>> 
>>> 
>>> 
>>> On Jun 22, 2014, at 12:06 AM, Manfred Lotz wrote:
>>> 
>>>> On Sat, 21 Jun 2014 10:12:27 -0400
>>>> "Alexander D. Knauth"
>>>> <alexander at knauth.org> wrote:
>>>> 
>>>>> The first thing I notice is that opt-max-size is a (Parameterof
>>>>> Any), but you use string->number on it. So you should probably
>>>>> put a guard on the opt-max-size parameter something like this: (:
>>>>> opt-max-size (Parameterof Any Real)) (: opt-max-size-guard (Any
>>>>> -> Real)) (define (opt-max-size-guard val)
>>>>> (cond [(real? val) val]
>>>>>       [(string? val) (opt-max-size-guard (string->number val))]
>>>>>       [else (error "Max size: must be a real number")]))
>>>>> 
>>>>> (define opt-max-size (make-parameter 0 opt-max-size-guard))
>>>>> 
>>>> 
>>>> Thanks for this, which works fine. I'm not qite sure I like this
>>>> as it seems to make the code more complicated. But if this is the
>>>> only possibility what can be done.
>>>> 
>>>> 
>>>> 
>>>>> On Jun 21, 2014, at 3:45 AM, Manfred Lotz
>>>>> <manfred.lotz at arcor.de> wrote:
>>>>> 
>>>>>> Hi there,
>>>>>> I try to change one of my programs to typed racket and fail
>>>>>> because of errors when dealing with command line arguments.
>>>>>> 
>>>>>> Here a minimum example:
>>>>>> 
>>>>>> #lang typed/racket/base
>>>>>> 
>>>>>> (require racket/cmdline)
>>>>>> 
>>>>>> 
>>>>>> (: opt-verbose-mode (Parameterof Boolean))
>>>>>> (define opt-verbose-mode (make-parameter #f))
>>>>>> (: opt-max-size (Parameterof Any))
>>>>>> (define opt-max-size (make-parameter "0"))
>>>>>> 
>>>>>> 
>>>>>> 
>>>>>> (define parms
>>>>>> (command-line
>>>>>> #:program "mypgm"
>>>>>> #:once-each
>>>>>> [("-v" "--verbose") "Show verbose output" (opt-verbose-mode #t)]
>>>>>> [("-m" "--max-size") NUM ("Max size." "NUM") (opt-max-size NUM)
>>>>>> ] ))
>>>>>> 
>>>>>> 
>>>>>> (define (myfun)
>>>>>> (when (opt-verbose-mode)
>>>>>>  (if (> (string->number (opt-max-size)) 0)
>>>>>>      #t
>>>>>>      #f)))
>>>>>> 
>>>>>> 
>>>>>> What do I do wrong? Any hint appreciated.
>>>>>> 
>>>>>> --
>>>>>> Thanks,
>>>>>> Manfred
>>>>>> 
>>>>>> 
>>>>>> 
>>>>>> 
> 
> 
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users


Posted on the users mailing list.