seanbunderwood
2021-7-22 12:43:05

Is there a drawing context that produces SVG?



seanbunderwood
2021-7-22 15:18:47

Thanks


agj_chile
2021-7-22 22:08:29

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…


sorawee
2021-7-22 22:11:50

Not sure if I understand your question correctly, but you can use or.


sorawee
2021-7-22 22:13:39

E.g.:

#lang typed/racket (: f : (U Number String Boolean) -> Number) (define (f x) (if (or (string? x) (boolean? x)) 1 x))


sorawee
2021-7-22 22:14:04

The conditional checks that it’s either a String or Boolean. I.e., (U String Boolean).


agj_chile
2021-7-22 22:36:43

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.



sorawee
2021-7-22 22:44:58

Or make-predicate


sorawee
2021-7-22 22:45:08

Depending on how you want to use it


sorawee
2021-7-22 22:46:16

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))


agj_chile
2021-7-22 22:49:18

Ooh, cool! Thanks! :smile:


agj_chile
2021-7-23 00:41:43

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))))


sorawee
2021-7-23 00:47:32

I haven’t taken a look at this closely, but want to note two things:

  1. The predicate library is deprecated, so you should not be using it (see https://github.com/jackfirth/predicates)
  2. 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?)


agj_chile
2021-7-23 00:57:13

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!


sorawee
2021-7-23 01:10:59

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.


agj_chile
2021-7-23 03:45:55

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


strika
2021-7-23 05:13:44

: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!


soegaard2
2021-7-23 05:15:58

The first thing to try is updating Racket to a newer version.


popa.bogdanp
2021-7-23 05:49:27

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


strika
2021-7-23 05:56:31

I see. I will try that. Thank you!