Others have tried to be more Socratic in pointing you this way, but here's
an attempt at being more explicit.

As you note, the result of `string->number` has the type `(U Complex
False)`. If we try to think about about this in a version of the HtDP
design recipe, we have a few cases:

   1. `string->number` produces an `Integer` when given an argument like
   `"42"`.
   2. `string->number` can also produce a value of any of the various
   `Number` types that aren't integers, as with `"3.2+6.0i"` or `"2/3"`.
   3. If the given string can't be parsed as any kind of number, like
   `"apple"`, `string->number` returns `#false`.

If you want to write a function with the type `(-> String Integer)`, you
are going to have to handle all of those cases, whether you use
`string->number` or not! What behavior makes sense in cases 2 and 3 (or
maybe it makes sense to break 2 into smaller cases) is going to depend on
your particular use case. Maybe it makes sense to return a default value.
Maybe you just want to raise an exception.

If we write a version of your `myconversion` function with a placeholder
(again in the spirit of HtDP), we might do something like this:
#lang typed/racket
(: myconversion (-> String Integer))
(define (myconversion str)
  (define rslt (string->number str))
  (cond
    [(exact-integer? rslt)
     rslt]
    [else
     ;; TODO
     ...]))

This is where Ben's suggestion of `exact-integer?` is useful: thanks to
Typed Racket's occurrence typing
<https://docs.racket-lang.org/ts-guide/occurrence-typing.html>, the type
system knows that, in the "then" branch of the conditional, `rslt` must
have the type `Integer` because it satisfied the `exact-integer?` test.
(Note that the `Integer` type corresponds to `exact-integer?`
<https://docs.racket-lang.org/ts-reference/type-ref.html#%28form._%28%28lib._typed-racket%2Fbase-env%2Fbase-types..rkt%29._.Integer%29%29>,
not `integer?`.)

We still need to fill in the "else" branch to handle the case that the
string didn't represent an integer. In your latter example, you raised an
exception, which is a sensible choice. Here's an idiomatic way to do that:
#lang typed/racket
(: myconversion (-> String Integer))
(define (myconversion str)
  (define rslt (string->number str))
  (cond
    [(exact-integer? rslt)
     rslt]
    [else
     (raise-argument-error 'myconversion
                           "a string representing an integer"
                           str)]))

One thing to observe is that this is exactly the way you would have
implemented such a function in untyped `#lang racket`: only the single type
annotation is different. Much of the reason is that Typed Racket works hard
through features like occurrence typing to be able to typecheck the kinds
of programs you would idiomatically write in untyped Racket. In this
particular case, though, it also reflects the fact that there isn't a type
for "strings that can be parsed as integers." There's a potential for
dynamic failure here that the static type system may help you to manage,
but ultimately can't escape. (Ignoring for the moment fancier type systems
that are mostly still research projects.)

Your problem with `sweet-exp` is a different one. I'm not familiar with
`sweet-exp`, so I can't really help you, but it looks like either a bug or
a known limitation in the concrete syntax `sweet-exp` supports. I will note
that the lambda function you use in that example would be a syntax error in
`#lang racket`.

-Philip


On Tue, Feb 11, 2020 at 10:04 AM Alain De Vos <devosalai...@gmail.com>
wrote:

> No exact-integer is a check it is not a type.
> The error described above remains
>
> On Tuesday, February 11, 2020 at 3:50:27 PM UTC+1, Alain De Vos wrote:
>>
>>
>>
>> On Tuesday, February 11, 2020 at 3:25:42 PM UTC+1, Ben Greenman wrote:
>>>
>>> You may want `exact-integer?`
>>> True , i should use exact-integer.
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/89f338be-f574-44b5-82d4-23f833ec14ac%40googlegroups.com
> <https://groups.google.com/d/msgid/racket-users/89f338be-f574-44b5-82d4-23f833ec14ac%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAH3z3gafGOGZt0p3Xw20-X2deT%3DTO8WW1-omgwBBiNrcq%3DLiVw%40mail.gmail.com.

Reply via email to