Is there a drawing context that produces SVG?
Thanks
Hi, everyone! I have a question regarding Typed Racket again. Is there a way to check for a union type? I know that primitive types and structs have predicates, but I haven’t found a way for union types…
Not sure if I understand your question correctly, but you can use or
.
E.g.:
#lang typed/racket
(: f : (U Number String Boolean) -> Number)
(define (f x)
(if (or (string? x) (boolean? x))
1
x))
The conditional checks that it’s either a String
or Boolean
. I.e., (U String Boolean)
.
Thanks, @sorawee! Yeah that’s what I meant and that’s exactly what I’m doing right now, but I was imagining that maybe there was a simpler way. Like if I define (define-type MyType (U String Number))
a way to easily check if a value is of type MyType
.
Or make-predicate
Depending on how you want to use it
E.g.,
#lang typed/racket
(define-type MyType (U String Boolean))
(define-predicate my-type? MyType)
(: f : (U Number String Boolean) -> Number)
(define (f x)
(if (my-type? x)
1
x))
Ooh, cool! Thanks! :smile:
I need some help with Typed Racket again… I can’t figure out why this isn’t working or what I’m supposed to do to require this predicate and get it to annotate the output: (require/typed
predicates
[listof? (All (A)
(-> (-> Any Boolean : A)
(-> Any Boolean : (Listof A))))])
I can’t understand the error to begin with: Type Checker: Type (All (A) (-> (-> Any Boolean : A) (-> Any Boolean : (Listof A)))) could not be converted to a contract: cannot generate contract for function type with props or objects. in: (All (A) (-> (-> Any Boolean : A) (-> Any Boolean : (Listof A))))
I haven’t taken a look at this closely, but want to note two things:
- The
predicate
library is deprecated, so you should not be using it (see https://github.com/jackfirth/predicates) - For basic types, can’t you use
define-predicate
instead?
(define-predicate my-type? (Listof (U String Boolean)))
(my-type? (list #t "abc" 1))
;; my-type? is equivalent to (listof? string? boolean?)
Thanks, I wasn’t aware that it was deprecated. :thinking_face: Yeah I can create my own predicate, I’ll do that. Though I’d still like it if someone who know’s what’s wrong with my code to explain it to me, as it looks like I’m missing something!
Consider:
(require/typed ... [my-cons (-> ... (Pairof ...))])
(define v (my-cons 1 2))
(car v)
Typed Racket can optimize the above code so that car
becomes unsafe-car
, which is much faster than car
, but would cause a crash if v
is actually not a Pair
. For this reason, Typed Racket really needs to make sure that my-cons
really produces a Pair
. The way to do this is to convert your type into a contract.
The problem seems to be that Typed Racket doesn’t know how to convert a function type with proposition into a contract. If you change (-> Any Boolean : A)
to (-> Any Boolean)
(and similarly for the (listof A)
proposition), I think it will work.
I see! :thinking_face: I mean it does work if I get rid of those annotations, but then I also lose the “occurrence typing”, which is what I was trying to achieve
:wave: I’m trying to make a standalone console application in Racket. It’s going well, but when I compile the application and move it to a system that doesn’t have Racket, I get the following error: open-input-file: cannot open input file
path: /home/strika/.racket/7.2/pkgs/cldr-bcp47/cldr/bcp47/data/timezone.xml
...
The error is coming from https://github.com/97jaz/cldr-bcp47/blob/master/cldr/bcp47/timezone.rkt#L36 as far as I know. I’m compiling the program with raco exe --orig-exe main.rkt
. I tried changing that to raco exe --orig-exe ++lib cldr/bcp47/timezone main.rkt
, but it didn’t help. Is there a way to compile the program so that it can be executed on a machine that doesn’t have Racket? Thank you!
The first thing to try is updating Racket to a newer version.
You need to pass the exe to “raco distribute” to build a distribution that will work on other machines: https://docs.racket-lang.org/raco/exe-dist.html\|https://docs.racket-lang.org/raco/exe-dist.html
I see. I will try that. Thank you!