querist
querist

Reputation: 654

Typed Racket string->number how to convert result to integer

I am trying to convert a project over from Racket to Typed Racket and I am encountering some issues with the typing mechanisms.

string->number returns (U Complex False), but I cannot find any procedures to convert that (or even just a Complex) to an Integer.

A very short example that illustrates my issue:

#language typed/racket

(define die : Integer 5)

(define dlist '("1" "2" "3" "4" "5"))

(set! die (string->number (car dlist)))

Thanks for your help!

Upvotes: 2

Views: 912

Answers (1)

Gibstick
Gibstick

Reputation: 737

The result of string->number has the type (U Complex False), where a value of false means string->number failed to parse a number. In this case you know for sure that it won't fail, so you can just assert that it is an integer with (assert (string->number ...) exact-integer?):

#lang typed/racket

(define die : Integer 5)

(define dlist '("1" "2" "3" "4" "5"))

(set! die (assert (string->number (car dlist)) exact-integer?))

I understand that it's supposed to be a simple example but try to stick with numbers and avoid strings if you can.

Upvotes: 2

Related Questions