How to convert from value with Any type to Real?

Eric Clack

I'm playing with Racket-Stamps, which is a mix of typed and regular Racket.

I'm writing a new feature and the code below attempts to call a function with a list of Reals, however because this list comes from untyped racket, it is actually a list of Any:

(define bounding (make-parameter '()))

;; snip

(when (not (empty? (bounding)))
  (let-values ([(x1 y1 x2 y2) (apply values (bounding))])
    (send pr set-bounding x1 y1 x2 y2)))

And in another file that calls the code above:

(bounding '(-20 -100 100 2))

Here's the error:

Type Checker: Bad arguments to function in `apply': Domains: a b ... b #f * Arguments: (Listof Any) * in: (apply values (bounding))

So how do I convert the Listof Any to a Listof Real?

Alex Knauth

The apply function here is given an arbitrary-length list as input, but the context expects exactly 4 values. If the list had any length other than 4, it would fail.

It seems like you meant for bounding to contain either the empty list or a list of exactly 4 real numbers.

(: bounding : (Parameterof (U Null (List Real Real Real Real))))
(define bounding (make-parameter '()))

Then every time your program tests whether the contents of (bounding) are empty and then relies on it being a list of 4 numbers, you need to put the value in a local variable first, so that Typed Racket sees the connection between the (not (empty? ...)) test and the use below it.

In other words, transform the pattern

(if (not (empty? (bounding)))
    (.... (bounding) ....)
    ....)

Into

(let ([bounding-v (bounding)])
  (if (not (empty? bounding-v))
      (.... bounding-v ....)
      ....))

In your example, that transformation gives:

(: bounding : (Parameterof (U Null (List Real Real Real Real))))
(define bounding (make-parameter '()))

....

(let ([bounding-v (bounding)])
   (when (not (empty? bounding-v))
     (let-values ([(x1 y1 x2 y2) (apply values bounding-v)])
       (send pr set-bounding x1 y1 x2 y2))))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert value from sklearn MinMaxScaler() back to real value?

How to unwrap an optional value from Any type?

Convert from type "any" to Object

How to convert a type to a value

How to fix 'Cannot convert value of type '[Any]' to type 'String' in coercion' error in swift

How to determine an interface{} value's "real" type?

Cannot convert value of type '[any Product]' to expected argument type '[Product]'

Cannot convert value of type '[String : Any]' to expected argument type 'String'

Cannot convert value of type 'NSMutableArray' to expected argument type '[Any]!'

Cannot convert value of type 'UIImage?' to expected argument type '[Any]'

Cannot convert value of type 'NSArray' to expected argument type '[Any]!'

Swift: Cannot convert value of type '[Any]' to specified type 'NSString'

Cannot convert value of type '[String:Any]?' to expected argument type '_?'

Cannot convert value of type '[String : Any]?' to expected argument type 'Float'

Cannot convert value of type 'x' to expected argument type '[String : Any]'

Cannot convert value of type 'String' to expected argument type '[Any]'

cannot convert value of type [Any Object] to expect argumet type 'HourlyTemperatures'

Swift - Cannot convert value of type 'Any?' to expected argument type 'String'

value of type String from the data source cannot be converted to type Real

How to convert any type into String in Julia

How to convert byte array to any type

how to convert python inferred type 'any' in java

IOS/Swift: How to indicate the first position of the $0 in the .firstIndex ? 'Cannot convert value of type 'Any' to expected argument type 'String''

How should i convert any type of date format from database to this format "d/m/Y "?

Convert a value with a regex to a real value

How to convert float[][] type array to "emxArray_real_T *x"

How to convert dynamic value to type value in expression

How to convert type when I hope to get different type value from one SharedPreferences key in Kotlin?

How to convert value from HTML table to JSON array in javascript excluding if any of the input in last row is empty